-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDatabaseProject1.java
More file actions
30 lines (28 loc) · 1.17 KB
/
DatabaseProject1.java
File metadata and controls
30 lines (28 loc) · 1.17 KB
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
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DatabaseProject1 {
public static void main(String[] args) {
DatabaseProject1 pro = new DatabaseProject1();
pro.createConnection();
}
void createConnection(){
try{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root", "root");
Statement stmt = con.createStatement();
// ResultSet rs = stmt.executeQuery("Select * from users");
ResultSet rs = stmt.executeQuery("Select * from users where name like 'A%'");
while(rs.next()){
String name = rs.getString("name");
System.out.println(name);
}
System.out.println("Database Connection Success");
}catch (ClassNotFoundException ex){
Logger.getLogger(DatabaseProject1.class.getName()).log(Level.SEVERE, null, ex);
}
catch (SQLException ex){
Logger.getLogger(DatabaseProject1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}