数据库某作业……
使用Tomcat server 运行

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
142
143
144
/**
* Created by Scarb on 4/24/2016.
*/

package DBHW3_Scarb;

import java.sql.*;

public class demo
{
public static void main(String[] args)throws ClassNotFoundException, SQLException
{
connDB conn = new connDB();
// 查询
ResultSet rs = conn.executeQuery("select * from Student");
while(rs.next())
{
System.out.print(rs.getString("Sno") + " ");
System.out.print(rs.getString("Ssex") + " ");
System.out.print(rs.getString("Sname") + " ");
System.out.print("\n");
}
// 删除
int updateNum = conn.executeUpdate("delete from Student where Sno = '2000104'");
rs = conn.executeQuery("select * from Student");
while(rs.next())
{
System.out.print(rs.getString("Sno") + " ");
System.out.print(rs.getString("Ssex") + " ");
System.out.print(rs.getString("Sname") + " ");
System.out.print("\n");
}
// 插入
updateNum = conn.executeUpdate("insert into Student values('2000000', 'Henry', '20', '11111')");
rs = conn.executeQuery("select * from Student");
while(rs.next())
{
System.out.print(rs.getString("Sno") + " ");
System.out.print(rs.getString("Ssex") + " ");
System.out.print(rs.getString("Sname") + " ");
System.out.print("\n");
}
}
}

class connDB
{
private static Connection conn = null;
private static Statement stmt = null;
private static ResultSet rs = null;
private static final String driverClass // 固定的值
= "com.microsoft.sqlserver.jdbc.SQLServerDriver";

private static final String URL // 所要连接的数据库
= "jdbc:sqlserver://localhost:1433;DatabaseName = Grade_Manager";

private static final String user = "Scarb";
private static final String pwd = "123456";

public connDB()
{
try
{
Class.forName(driverClass);
conn = DriverManager.getConnection(URL, user, pwd); // 创建一个数据库连接
}
catch (Exception e)
{
e.printStackTrace();
}
}

public ResultSet executeQuery(String sql) // 数据库操作
{
try
{
// 创建Statement对象,用来向数据库发送查询和命令
// 它由Connection的createStatement方法创建
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch (SQLException ex)
{
System.err.println(ex.getMessage());
}
return rs;
}

// 结果集处理,是哟弄个Result的next方法在表中移动
public int executeUpdate_id(String sql)
{
int result = 0;
try
{
stmt = conn.createStatement(1004, 1007);
result = stmt.executeUpdate(sql);
String ID = "select @@IDENTITY as id";
rs = stmt.executeQuery(ID);
if(rs.next()) // 在表中移动,每次移动一行,返回以id为索引的值
{
int autoID = rs.getInt("id");
result = autoID;
}
}
catch (SQLException ex)
{
result = 0;
}
return result;
}

public int executeUpdate(String sql) // update
{
int result = 0;
try
{
stmt = conn.createStatement();
result = stmt.executeUpdate(sql);
}
catch (SQLException ex)
{
result = 0;
}
return result;
}

// 关闭连接,关闭连接的同时,还要关闭对应的Statement和ResultSet对象
public void close()
{
try
{
if(rs != null)
rs.close(); // 关闭ResultSet
if(stmt != null)
stmt.close(); // 关闭Statement
if(conn != null)
conn.close(); // 关闭连接
}
catch(Exception e)
{
e.printStackTrace(System.err);
}
}
}