-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJDBC
More file actions
38 lines (27 loc) · 897 Bytes
/
JDBC
File metadata and controls
38 lines (27 loc) · 897 Bytes
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
For connecting Java to the Database ..
!.Import packages
2. Load driver
3.Register Driver
4.Create connection
5.Create Statement
6.execute
then Response will be shown
Can be done my any DBMS such as mysql, postgresql.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
public class Demo {
public static void main(String[] args) throws Exception {
String sql = "select name from product where id=8";
String url ="jdbc:postgresql://localhost:5432/telusko";
String username = "postgres";
String password = "0000";
Connection con = DriverManager.getConnection(url, username, password);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sql);
rs.next();
String name = rs.getString( columnIndex:1);
System.out.println(name);
}
}