Repository navigation
Expand file tree
/
Copy pathmysqlDemo.java
More file actions
58 lines (55 loc) · 1.65 KB
/
Copy pathmysqlDemo.java
File metadata and controls
58 lines (55 loc) · 1.65 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
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
56
57
58
package com.test.basic.practice;
import java.sql.*;
/**
* Created by beigui on 2016/4/9.
* @author xueguiyang & weiyuxin
*/
public class mysqlDemo {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String url ="jdbc:mysql://127.0.0.1:3306/test";
String username = "root";
String password = "password";
String mysql = "Select * from users";
ResultSet rs = null;
PreparedStatement state = null;
Connection conn = null;
try {
conn = DriverManager.getConnection(url,username,password);
state = conn.prepareStatement(mysql);
rs= state.executeQuery();
while(rs.next()){
Data data = new Data(rs.getString("username"),rs.getString(password));
System.out.println(data);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (null != rs) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (null != state){
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (null != conn){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}