数据库宿舍管理系统
C/S結構設計數據庫宿舍管理系統
主要利用Java.swing與sql server設計的宿舍管理系統,存在一些問題歡迎指正。
數據庫設置
連接數據庫(JDBC)
如何連接數據庫請看:https://blog.csdn.net/weixin_45330449/article/details/107207913
package GUIdemo1;import java.sql.Connection; import java.sql.DriverManager;public class DBconnect {Connection dbConn;public DBconnect(){try {Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");String dbURL = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=SSGL";dbConn = DriverManager.getConnection(dbURL, "sa", "sa");}catch(Exception ei) {ei.printStackTrace();System.out.println("連接數據庫失敗!");}} }界面設置
mainDisp類:
dormSearch類:
class dormSearch extends JDialog implements ActionListener {private JTextField jt;private JButton jb;private JComboBox cmb;private JPanel jp;private JTable table;private DefaultTableModel model;private DBconnect DB = new DBconnect();private String[] srr={"---請選擇查詢方式---","按宿舍查詢","按學號查詢"};public dormSearch(){super();setLayout(new BorderLayout());setTitle("入住信息查詢");setSize(500,300);setLocationRelativeTo(null);jt=new JTextField(10);jb = new JButton("查詢");jp = new JPanel();model=new DefaultTableModel();table=new JTable();table.setModel(model);JScrollPane jsp=new JScrollPane(table);cmb = new JComboBox(srr);add(jp,BorderLayout.NORTH);add(jsp,BorderLayout.CENTER);jp.add(cmb);jp.add(jt);jp.add(jb);jb.addActionListener(this);setVisible(true);}public void actionPerformed(ActionEvent e) {if (cmb.getSelectedIndex()==2) {model.setRowCount(0);model.setColumnCount(0);try {String str1=jt.getText();String sql = "select * from stay_info where Stu_num="+str1;Statement statement = DB.dbConn.createStatement();ResultSet res=statement.executeQuery(sql);//獲取表中列數及列名,作為表格組件的標題ResultSetMetaData rsmd=res.getMetaData();//獲得列數int count=rsmd.getColumnCount();將列名添加到表格模型作為標題for(int i=1;i<=count;i++){model.addColumn(rsmd.getColumnName(i));}String[] row=new String[count];while(res.next()){for(int i=0;i<count;i++)row[i]=res.getString(i+1);//增加一行model.addRow(row);}res.close();} catch (Exception ei) {ei.printStackTrace();}}else if (cmb.getSelectedIndex()==1){model.setRowCount(0);model.setColumnCount(0);try {String str1=jt.getText();String sql="select * from stay_info where Dorm_num="+str1;Statement statement=DB.dbConn.createStatement();ResultSet res=statement.executeQuery(sql);//獲取表中列數及列名,作為表格組件的標題ResultSetMetaData rsmd=res.getMetaData();//獲得列數int count=rsmd.getColumnCount();將列名添加到表格模型作為標題for(int i=1;i<=count;i++){model.addColumn(rsmd.getColumnName(i));}String[] row=new String[count];while(res.next()){for(int i=0;i<count;i++)row[i]=res.getString(i+1);//增加一行model.addRow(row);}res.close();}catch(Exception ei) {ei.printStackTrace();}}} }效果:
程序下載:https://download.csdn.net/download/weixin_45330449/12589569
GitHub下載:https://github.com/As-Zach/Demo/blob/master/GUIdemo.zip
總結
- 上一篇: 5安卓输入法键盘显示 搜索_手机输入法谁
- 下一篇: 数据库系统的特点