-
Notifications
You must be signed in to change notification settings - Fork 2
/
ConnectDB.java
52 lines (42 loc) · 1.94 KB
/
ConnectDB.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
import java.sql.*;
import java.util.ResourceBundle;
// For testing connection to the postgres database
public class ConnectDB {
public static void main(String[] args) throws Exception {
ResourceBundle rd = ResourceBundle.getBundle("config");
String url = rd.getString("url"); // localhost:5432
String username = rd.getString("username");
String password = rd.getString("password");
// 1
String query = "select * from ac_cc";
// 2
// int userid = 1;
// String uname = "Aman";
// String query = "insert into student values (" + userid + ", '" + uname + "')";
// 3
// String query = "insert into student values (?,?)";
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection(url, username, password);
DatabaseMetaData dbm = con.getMetaData();
ResultSet ch = dbm.getTables(null, null, "ac_cc", new String[] {"TABLE"});
System.out.println(ch.next());
// 1
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query);
while(rs.next()){
int id = rs.getInt("berth_no");
String name = rs.getString("type");
System.out.println(id + " - " + name);
}
// 2
// int cnt = st.executeUpdate(query);
// System.out.println(cnt + " rows affected");
// 3
// PreparedStatement st = con.prepareStatement(query);
// st.setInt(1, userid);
// st.setString(2, uname);
// int cnt = st.executeUpdate();
st.close();
con.close();
}
}