java宿舍管理系统书籍_JAVA学生宿舍管理系统

需要的工具

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 (数据库操作,包括连接、断开、增删改查等操作)

一、登陆界面及代码

c167fbe2e8eead89bec917d3cb7c0e1b.png

部分代码:

登陆按钮事件

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

二、系统管理界面及代码**(这里只添加了俩个表,多少表都是一样的道理**)

增加数据:点击增加按钮会出现一空白行,填写完点击保存即可

保存(更新)数据:直接在表中进行相应的操作,再点击保存即可

删除数据:选择某行,点击删除按钮即可

b29e12dc7ad8ac82d738bd63f6b85bb6.png

1d23881e79c6d59bb6671796d5826b03.png

部分代码:

//--------------------------------------按钮事件--------------------------------

@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

535036d47e67cc367d2318ed98e780d2.png

这个是用户表结构和登陆账号和密码(可自行修改)

0de92ed98324a6e42fc7a693941f6b2b.png

1e7daa98f78be544955e60529139278f.png

**

宿舍信息表结构**

f85673998a6f64d40325cd537a89cb37.png

**

学生信息表结构**

cb6c8461ea416878c2e73ccba97581ef.png

## 需要源码,点赞关注截图,发邮箱到1397195447@qq.com

文章来源: blog.csdn.net,作者:喳喳怪,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq_36868951/article/details/86601832

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值