-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJDBC.java
More file actions
78 lines (65 loc) · 2.24 KB
/
JDBC.java
File metadata and controls
78 lines (65 loc) · 2.24 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
73
74
75
76
77
78
package hhh;
import java.sql.*;
public class JDBC {
/*
public static void main(String[] args) {
String JDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";// SQL数据库引擎
String connectDB = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=serverHHH";// 数据源
try {
Class.forName(JDriver);// 加载数据库引擎,返回给定字符串名的类
} catch (ClassNotFoundException e) {
// e.printStackTrace();
System.out.println("加载数据库引擎失败");
System.exit(0);
}
System.out.println("数据库驱动成功");
try {
String user = "hehuihui";
String password = "hehuihui";
Connection con = DriverManager.getConnection(connectDB, user, password);// 连接数据库对象
System.out.println("连接数据库成功");
Statement stmt = con.createStatement();// 创建SQL命令对象
String a1="ffffff";
//'"+name+"','"+age+"')");
String a2 = "INSERT INTO ServerTable(event) VALUES('"+a1+"')";
stmt.executeUpdate(a2);
// 关闭连接
stmt.close();// 关闭命令对象连接
con.close();// 关闭数据库连接
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接错误");
System.exit(0);
}
}
*/
public void insert(String a1)
{
String JDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";// SQL数据库引擎
String connectDB = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=serverHHH";// 数据源
try {
Class.forName(JDriver);// 加载数据库引擎,返回给定字符串名的类
} catch (ClassNotFoundException e) {
// e.printStackTrace();
System.out.println("加载数据库引擎失败");
System.exit(0);
}
System.out.println("数据库驱动成功");
try {
String user = "hehuihui";
String password = "hehuihui";
Connection con = DriverManager.getConnection(connectDB, user, password);// 连接数据库对象
System.out.println("连接数据库成功");
Statement stmt = con.createStatement();// 创建SQL命令对象
String a2 = "INSERT INTO ServerTable(event) VALUES('"+a1+"')";
stmt.executeUpdate(a2);
// 关闭连接
stmt.close();// 关闭命令对象连接
con.close();// 关闭数据库连接
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接错误");
System.exit(0);
}
}
}