-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathsimpleJDBCPostgres.java
140 lines (118 loc) · 4.59 KB
/
simpleJDBCPostgres.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
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
/**
* Make sure the Postgresql JDBC driver is in your classpath.
* You can download the JDBC 4 driver from here if required.
* https://jdbc.postgresql.org/download.html
*
* take care of the variables usernamestring and passwordstring to use
* appropriate database credentials before you compile !
*
**/
import java.sql.* ;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
class simpleJDBC
{
public static void main ( String [ ] args ) throws SQLException
{
// Unique table names. Either the user supplies a unique identifier as a command line argument, or the program makes one up.
String tableName = "";
int sqlCode=0; // Variable to hold SQLCODE
String sqlState="00000"; // Variable to hold SQLSTATE
if ( args.length > 0 ){
tableName += args [ 0 ] ;
}
else {
tableName += "example3.tbl";
}
// Register the driver. You must register the driver before you can use it.
try {
DriverManager.registerDriver ( new org.postgresql.Driver() ) ;
} catch (Exception cnfe){
System.out.println("Class not found");
}
// This is the url you must use for Postgresql.
//Note: This url may not valid now !
String url = "jdbc:postgresql://comp421.cs.mcgill.ca:5432/cs421";
Connection con = DriverManager.getConnection (url,usernamestring, passwordstring) ;
Statement statement = con.createStatement ( ) ;
// Creating a table
try {
String createSQL = "CREATE TABLE " + tableName + " (id INTEGER, name VARCHAR (25)) ";
System.out.println (createSQL ) ;
statement.executeUpdate (createSQL ) ;
System.out.println ("DONE");
}catch (SQLException e)
{
sqlCode = e.getErrorCode(); // Get SQLCODE
sqlState = e.getSQLState(); // Get SQLSTATE
// Your code to handle errors comes here;
// something more meaningful than a print would be good
System.out.println("Code: " + sqlCode + " sqlState: " + sqlState);
}
// Inserting Data into the table
try {
String insertSQL = "INSERT INTO " + tableName + " VALUES ( 1 , \'Vicki\' ) " ;
System.out.println ( insertSQL ) ;
statement.executeUpdate ( insertSQL ) ;
System.out.println ( "DONE" ) ;
insertSQL = "INSERT INTO " + tableName + " VALUES ( 2 , \'Vera\' ) " ;
System.out.println ( insertSQL ) ;
statement.executeUpdate ( insertSQL ) ;
System.out.println ( "DONE" ) ;
insertSQL = "INSERT INTO " + tableName + " VALUES ( 3 , \'Franca\' ) " ;
System.out.println ( insertSQL ) ;
statement.executeUpdate ( insertSQL ) ;
System.out.println ( "DONE" ) ;
} catch (SQLException e)
{
sqlCode = e.getErrorCode(); // Get SQLCODE
sqlState = e.getSQLState(); // Get SQLSTATE
// Your code to handle errors comes here;
// something more meaningful than a print would be good
System.out.println("Code: " + sqlCode + " sqlState: " + sqlState);
}
// Querying a table
try {
String querySQL = "SELECT id, name from " + tableName + " WHERE NAME = \'Vicki\'";
System.out.println (querySQL) ;
java.sql.ResultSet rs = statement.executeQuery ( querySQL ) ;
while ( rs.next ( ) ) {
int id = rs.getInt ( 1 ) ;
String name = rs.getString (2);
System.out.println ("id: " + id);
System.out.println ("name: " + name);
}
System.out.println ("DONE");
} catch (SQLException e)
{
sqlCode = e.getErrorCode(); // Get SQLCODE
sqlState = e.getSQLState(); // Get SQLSTATE
// Your code to handle errors comes here;
// something more meaningful than a print would be good
System.out.println("Code: " + sqlCode + " sqlState: " + sqlState);
}
//Updating a table
try {
String updateSQL = "UPDATE " + tableName + " SET NAME = \'Mimi\' WHERE id = 3";
System.out.println(updateSQL);
statement.executeUpdate(updateSQL);
System.out.println("DONE");
// Dropping a table
String dropSQL = "DROP TABLE " + tableName;
System.out.println ( dropSQL ) ;
statement.executeUpdate ( dropSQL ) ;
System.out.println ("DONE");
} catch (SQLException e)
{
sqlCode = e.getErrorCode(); // Get SQLCODE
sqlState = e.getSQLState(); // Get SQLSTATE
// Your code to handle errors comes here;
// something more meaningful than a print would be good
System.out.println("Code: " + sqlCode + " sqlState: " + sqlState);
}
// Finally but importantly close the statement and connection
statement.close ( ) ;
con.close ( ) ;
}
}