需要的工具
1.SQL Server
2.Eclipse
3.JDBC连接数据库驱动
https://download.microsoft.com/download/A/F/B/AFB381FF-7037-46CE-AF9B-6B1875EA81D7/sqljdbc_6.0.8112.200_chs.exe
功能实现
分为三大类
1.land.java (登陆界面)
2.system.java (系统管理界面)
3.sql.java (数据库操作,包括连接、断开、增删改查等操作)
一、登陆界面及代码
部分代码:
登陆按钮事件
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if((JButton)e.getSource()== buttonLand)
{ String name = textName.getText().trim();
String password = String.valueOf(textPassword.getPassword()).trim(); int num = sq.landing(name, password); if(num==1) { JOptionPane.showMessageDialog(frame, "欢迎进入学生宿舍管理系统!","提示:",JOptionPane.PLAIN_MESSAGE); system system = new system(); frame.dispose(); } else { JOptionPane.showMessageDialog(frame, "账号或者密码错误!","提示:",JOptionPane.ERROR_MESSAGE); }
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
二、系统管理界面及代码**(这里只添加了俩个表,多少表都是一样的道理**)
增加数据:点击增加按钮会出现一空白行,填写完点击保存即可
保存(更新)数据:直接在表中进行相应的操作,再点击保存即可
删除数据:选择某行,点击删除按钮即可
部分代码:
//--------------------------------------按钮事件--------------------------------
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//------------------------------- 删除-----------------------------
String tableName = null;
String key1 = null;
if((JButton)e.getSource()==buttonDelete)
{ if(paneParent.getSelectedIndex()==1) { tableName = "学生信息表"; key1 = "sno"; } if(paneParent.getSelectedIndex()==2) { tableName = "宿舍信息表"; key1 = "dno"; } int row = table.getSelectedRow(); if(row!=-1) { String key2 = (String) tableModel.getValueAt(row, 0); int result = JOptionPane.showConfirmDialog(null, "确定要删除吗?","请确认",JOptionPane.YES_NO_OPTION); if(result==JOptionPane.OK_OPTION) { String sql = "delete from "+tableName+" where "+key1+"="+key2; int num = sq.delete(sql); if(num>0) { tableModel.removeRow(row); } } } else { JOptionPane.showMessageDialog(null, "请选择要删除的行!","提示:",JOptionPane.ERROR_MESSAGE); }
}
//------------------------------保存------------------------
if((JButton)e.getSource()==buttonSave)
{ int result = JOptionPane.showConfirmDialog(null, "请确认数值已经更改,否则保存无效","请确认",JOptionPane.YES_NO_OPTION); if(result==JOptionPane.OK_OPTION) { int row = table.getRowCount(); int column = table.getColumnCount(); String[][]valueRow= new String[row][column]; String[] sqlvalue = new String[row]; for(int i = 0; i < row; i++) { for(int j = 0; j < column; j++) { valueRow[i][j] = table.getValueAt(i, j).toString(); } } if(paneParent.getSelectedIndex()==1) { for(int i =0;i
//------------------------------增加---------------------------
if((JButton)e.getSource()==buttonIncrease)
{ tableModel.addRow(new Vector<>());
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
三、sql.java (重要)
public class SQL { public Connection conn; public Statement st; public ResultSet rs; //----------------------------获取链接--------------------------------
public Connection getConn() throws SQLException, ClassNotFoundException
{
String driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; String url = "jdbc:sqlserver://localhost:1433;databaseName=学习宿舍";
//:1433为数据库默认端口号,学习宿舍为数据库名字
String user = "sa"; //登录用户名
String password = "123456"; //登录密码
try { Class.forName(driverClassName); conn = DriverManager.getConnection(url, user, password); System.out.println("数据库连接成功");
} catch (SQLException ex1)
{ System.out.println("数据库连接失败");
} return conn;
}
//-----------------------------------关闭链接----------------------------------
public void Close()
{
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
st.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//----------------------------------------登陆---------------------------------
public int landing(String name1,String password1)
{
int num = 0;
String sql = "select *from 用户表";
try{
getConn();
st = conn.createStatement();
rs = st.executeQuery(sql);
while(rs.next())
{ String name = rs.getString(1).trim(); String password = rs.getString(2).trim(); if(name.equals(name1)&&password.equals(password1)) { num = 1; } }
}catch (SQLException e) {
// TODO: handle exception
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Close();
return num;
}
//--------------------------------查询-------------------------------
public Vector> query(String tableName,int column)
{
int num = 0;
String sql = "select *from "+tableName;
Vector> data = new Vector>();
try{
getConn();
st = conn.createStatement();
rs=st.executeQuery(sql);
while(rs.next())
{ Vector rowdata = new Vector(); for(num=1;num<=column;num++) { rowdata.add(rs.getString(num)); } data.add(rowdata);
} }catch(SQLException ex1)
{
System.out.println("失败"+ex1);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Close();
return data;
}
//---------------------------------------删除----------------------------------
public int delete(String sql)
{
int num = 0;
try{
getConn();
st = conn.createStatement(); num = st.executeUpdate(sql);
}catch (SQLException e) {
// TODO: handle exception
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Close();
return num;
}
//-------------------------------------保存-------------------------------------
public Vector> Save(String[] sqlvalue,String tableName,int row,int column)
{
Vector> data = new Vector>();
try{
getConn();
st = conn.createStatement(); st.executeUpdate("delete from "+tableName);
for(int i =0;i
{ st.executeUpdate(sqlvalue[i].toString());
} data = query(tableName, column); }catch (SQLException e) {
// TODO: handle exception
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return data;
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
数据库连接账号密码及表名
**
这个是数据库的连接账号和密码123456
这个是用户表结构和登陆账号和密码(可自行修改)
**
宿舍信息表结构**
**
学生信息表结构**
## 需要源码,点赞关注截图,发邮箱到1397195447@qq.com
文章来源: blog.csdn.net,作者:喳喳怪,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq_36868951/article/details/86601832