Skip to content

Commit 07f20ea

Browse files
authored
JDBC demonstration
1 parent 06ca7d4 commit 07f20ea

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

JDBC/com/gces/JDBC/JDBCDemo.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.gces.JDBC;
2+
3+
import java.sql.Connection;
4+
import java.sql.ResultSet;
5+
import java.sql.DriverManager;
6+
import java.sql.SQLException;
7+
import java.sql.Statement;
8+
9+
public class JDBCDemo {
10+
11+
public static void main(String[] args) {
12+
String sql = "SELECT * from employees";
13+
14+
try {
15+
// step1 : Load and Register Driver
16+
Class.forName("com.mysql.cj.jdbc.Driver");
17+
18+
//step 2 : Establish connection between apps and db
19+
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/gces","root",""); //3306 mysql ko default port ho
20+
21+
//step 3: creation of Statement object
22+
Statement st = con.createStatement();
23+
24+
//step 4 : send and execute SQL query
25+
26+
ResultSet rs = st.executeQuery(sql);
27+
28+
//step 5 : process result from ResultSet
29+
30+
while(rs.next())
31+
{
32+
System.out.println(rs.getInt(1));
33+
System.out.println(rs.getString(2));
34+
System.out.println(rs.getString(3));
35+
System.out.println(rs.getFloat(4));
36+
}
37+
38+
//step 6 : close connection
39+
con.close();
40+
41+
} catch (ClassNotFoundException | SQLException e) {
42+
e.printStackTrace();
43+
}
44+
}
45+
46+
}

0 commit comments

Comments
 (0)