-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathSQL.java
61 lines (45 loc) · 1.56 KB
/
SQL.java
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
package comp421;
import java.sql.* ;
// done
public class SQL {
Statement statement;
public SQL() throws SQLException
{
try {
DriverManager.registerDriver ( new org.postgresql.Driver() ) ;
} catch (Exception cnfe){
System.out.println("Class not found");
}
String url = "jdbc:postgresql://comp421.cs.mcgill.ca:5432/cs421";
Connection con = DriverManager.getConnection (url, "cs421g27", "Mcgill1234") ;
statement = con.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE ) ;
System.out.println("Build connection with database successfully");
// pointer move availible
}
public void WriteExcute(String sqlCode)
{
try {
statement.executeUpdate ( sqlCode ) ;
System.out.println("Write excute the code "+sqlCode);
} catch (SQLException e)
{
int errorCode = e.getErrorCode(); // Get SQLCODE
String sqlState = e.getSQLState(); // Get SQLSTATE
System.out.println("Code: " + sqlCode + " sqlState: " + sqlState);
}
}
public java.sql.ResultSet QueryExchte(String sqlCode)
{
java.sql.ResultSet rs = null;
try {
rs = statement.executeQuery (sqlCode ) ;
System.out.println("Query excute the code "+sqlCode);
} catch (SQLException e)
{
int errorCode = e.getErrorCode(); // Get SQLCODE
String sqlState = e.getSQLState(); // Get SQLSTATE
System.out.println("Code: " + sqlCode + " sqlState: " + sqlState);
}
return rs;
}
}