-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnMysql.java
More file actions
32 lines (28 loc) · 949 Bytes
/
Copy pathConnMysql.java
File metadata and controls
32 lines (28 loc) · 949 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
package mysql;
import java.sql.*;
/**
* @Deacription: 连接数据库的简单练习
* @author: geguangfu
* @Date: 2017/10/29
* @Time: 14:25
* @Version: 1.0
*/
public class ConnMysql {
public static void main(String[] args)
throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver"); // 注册驱动,需要导包
String user = "root"; // 用户名
String pwd = "root"; // 密码
String url = "jdbc:mysql://localhost:3306/scott"; // 数据库
Connection conn = DriverManager.getConnection(url, user, pwd); // 获取连接
System.out.println(conn);
String sql = "SELECT * FROM user";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
String name = rs.getString("name");
System.out.println(name);
}
conn.close();
}
}