-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDb.java
More file actions
72 lines (65 loc) · 1.72 KB
/
Db.java
File metadata and controls
72 lines (65 loc) · 1.72 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
66
67
68
69
70
71
72
package db;
import java.sql.*;
public class Db {
private static String url = "jdbc:mysql://192.168.99.100:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false";
private static String use = "root";
private static String passwd = "123456";
private static Connection con = null;
static{
try{
//step1:动态加载驱动
Class.forName("com.mysql.jdbc.Driver");
//step2:连接数据库
con = DriverManager.getConnection(url, use, passwd);
}catch(ClassNotFoundException e)
{
System.out.println("加载mysql驱动失败");
}catch(SQLException e)
{
System.out.println("连接数据库失败");
}
}
public static Connection getCon()
{
return con;
}
public static void main(String[] args){
try{
//step3:创建语句
Statement stt = con.createStatement();
//3.1查询
String sql = "select * from goddess";
ResultSet res = stt.executeQuery(sql);
while(res.next())
{
System.out.println("记录:"+res.getInt("id")+" "+res.getString("name")+" "+res.getInt("age")+" "+res.getString("phone")+" "+res.getDate("addtime"));
}
//3.2添加
//stt.executeQuery("set names utf8");
// String sql = "insert into goddess values(null,'娜娜',30,'18645542233')";
// int rs = stt.executeUpdate(sql);
// if(rs!=-1)
// {
// System.out.println("插入成功!");
// }else
// {
// System.out.println("插入失败!");
// }
//
//3.3更新
// String sql = "update goddess set age=28";
// int rs = stt.executeUpdate(sql);
// System.out.println("更新数量:"+rs);
// if(rs!=-1)
// {
// System.out.println("更新成功!");
// }else
// {
// System.out.println("更新失败!");
// }
}catch(SQLException e)
{
System.out.println(e);
}
}
}