-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL.java
More file actions
65 lines (62 loc) · 2.1 KB
/
SQL.java
File metadata and controls
65 lines (62 loc) · 2.1 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
59
60
61
62
63
64
65
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
class SQL {
public static void main(String args[]) {
//声明Connection对象
Connection con;
//驱动程序名
String driver = "com.mysql.jdbc.Driver";
//URL指向要访问的数据库
String url = "jdbc:mysql://localhost:3306/StudentDataBase";
//Mysql配置时的用户名
String user = "root";
//mysql配置时的密码
String password = "gaoxing12.21";
//遍历查询结果集
try {
//加载驱动程序
Class.forName(driver);
//1.getConnection()方法,连接Mysql数据库
con = DriverManager.getConnection(url,user,password);
if(!con.isClosed())
System.out.println("Succeeded connection to the database");
//2.创建statement类对象,用来执行SQL语句
Statement statement = con.createStatement();
//要执行的SQL语句
String sql = "select * from User";
//3.ResultSet类,用来存放获取的结果集
ResultSet rs = statement.executeQuery(sql);
System.out.println("----------------------------------");
System.out.println("执行结果如下所示:");
System.out.println("----------------------------------");
System.out.println("姓名"+"\t"+"职称");
System.out.println("-----------------------------------");
String job = null;
String id = null;
while (rs.next()) {
//获取stuname这列数据
job = rs.getString(1);
//获取stuid这列数据
id = rs.getString("password");
//输出结果
System.out.println(job+"\t"+id);
}
rs.close();
con.close();
} catch (ClassNotFoundException e) {
//数据库驱动类异常
System.out.println("Sorry cann't find the Driver");
e.printStackTrace();
} catch(SQLException e){
//数据库连接失败异常处理
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println("数据库数据成功获取");
}
}
}