-
Notifications
You must be signed in to change notification settings - Fork 0
/
jdbc.java
55 lines (32 loc) · 1.38 KB
/
jdbc.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
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class jdbc {
private static final String URL_name="jdbc:mysql://localhost:3306/test";
private static final String Uname="root";
private static final String upass="root";
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection(URL_name,Uname,upass);
Statement smt = con.createStatement();
// smt.executeUpdate("CREATE TABLE myTABLE("+"Name VARCHAR(10),"+"age INT)");
System.out.println("Table created successfully...");
PreparedStatement pst=con.prepareStatement("INSERT into myTABLE values(?,?)");
pst.setString(1, "vishwa");
pst.setInt(2,20);
int r=pst.executeUpdate();
System.out.println(r+" rows affected");
ResultSet rs=pst.executeQuery("SELECT * From myTABLE");
while(rs.next()){
String name=rs.getString(1);
int age=rs.getInt(2);
System.out.println("Name: "+name +"AGE: "+age);
}
} catch (Exception e) {
System.out.println(e);
}
}
}