con null pstmt null rs null stmt null SQLServerDataSource dsm new SQLS

 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
con = null;
pstmt = null;
rs = null;
stmt = null;
SQLServerDataSource dsm = new SQLServerDataSource();
dsm.setUser("tolya");
dsm.setPassword("123456");
dsm.setServerName("HUNTER-ПК");
dsm.setPortNumber(1433);
dsm.setDatabaseName("OD");
con = dsm.getConnection();
System.out.println("Connected to MS SQL successfully.");
sql = "CREATE TABLE Person(" +
"name VARCHAR(20) NOT NULL, " +
"telephone VARCHAR(20) NOT NULL " +
")";
// CREATE
stmt = connection.createStatement();
ps = connection.prepareStatement(sql);
ps.execute();
// INSERT
ps = connection.prepareStatement("INSERT INTO Person (name, telephone) VALUES (?, ?)");
ps.setString(1,"Temp");
ps.setString(2,"0993812832");
ps.execute();
// UPDATE
ps = connection.prepareStatement("UPDATE Person SET name = ? WHERE name = 'Temp'");
ps.setString(1,"Tolya");
ps.execute();
// SELECT
rset = stmt.executeQuery("SELECT * FROM Person");
while (rset.next())
System.out.println(rset.getString("name")+ " : " + rset.getString("telephone"));
stmt.close();
// DELETE
ps = connection.prepareStatement("DELETE FROM Person WHERE name = ?");
ps.setString(1,"Tolya");
ps.execute();
// DROP
stmt = connection.createStatement();
ps = connection.prepareStatement("DROP TABLE Person") ;
ps.executeUpdate();