JDBC基础知识

date: 2018/4/8 1:33:12


JDBC基础知识文档整理



文章转载苏鹏


JDBC基础知识


1.    JDBC基本概念
1.1.    java提供的一组连接操作数据库的标准API(编程接口),它定义了一组规范,规定了java程序和数据库连接时的操作标准,每个数据库生成厂商按照这个标准来完成具体的操作方法,它是java程序和数据库之间的桥梁
1.2.    好处:程序员不用关心不同的数据库在连接时有什么不同,只要按着JDBC的规范要求去操作就可以完成数据库的连接和操作
1.3.    好处:在需要更换数据库时,对项目代码影响非常小,只需要做简单的改动就可以连接到不同的数据库

2.    JDBC核心API位于java.sql包中

2.1.    DriverManager :数据库驱动的管理者
2.2.    Connection :数据库连接  
2.3.    Statement :数据库操作
2.4.    PreparedStatement:预编译的数据库操作
2.5.    ResultSet:保存查询语句的结果(结果集)

3.    JDBC准备工作
3.1.    连接SQL Server前需要先打开数据库的TCP/IT协议
3.2.    在项目中导入数据库jar包

4.    连接数据库
4.1.    通过反射机制加载驱动
4.2.    调用驱动管理者创建连接对象
4.3.    通过连接对象创建数据库操作对象

5.    SQL注入  ‘ or ‘1’=’1
5.1.    PreparedStatement---先编译sql语句,再传入具体值,限制了sql语句的格式和条件

6.    整理重构JDBC代码
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
package android.xiaolan.sql.tools.BD;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 数据库封装
* @author 烟花小神
*
*/
public class DB {
// 连接
Connection connection;
// 操作
PreparedStatement pd;
// 连接地址
final String url = "jdbc:sqlserver://localhost;database=student1703";
// 连接账号
final String user = "sa";
// 连接密码
final String password = "lan666";
/***
* @data 2018年4月5日15:29:56
* @param sql 传入的是 SQL命令
* @return
*/
public PreparedStatement GetConn(String sql) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
connection = DriverManager.getConnection(url, user, password);
// 操作
pd = connection.prepareStatement(sql);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
System.out.println("数据库连接失败");
e.printStackTrace();
}
return pd;
}
/***
* 关闭数据库连接
* @data 2018年4月5日15:28:07
* @param 方法传入的是结果集
*/
public void close(ResultSet RT) {
try {
if (RT != null) {
RT.close();
}
if (pd != null) {
pd.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

增删改查


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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package android.xiaolan.sql.tools.Stu;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import android.xiaolan.sql.tools.BD.DB;
/**
* 数据库增删改查
* @author 烟花小神
*
*/
public class StuDao {
//返回的行数
int updata ;
//数据库封装对象
DB db = new DB();
//操作对象
PreparedStatement pd;
//添加一行数据
public int save(StuInfo stu){
String sql = "insert into StuInfo(stuid,name,age,sex,groups,dorm,tel) values(?,?,?,?,?,?,?)";
pd = db.GetConn(sql);
try {
pd.setString(1, stu.stuid);
pd.setString(2, stu.name);
pd.setInt(3, stu.age);
pd.setString(4, stu.sex);
pd.setString(5, stu.groups);
pd.setString(6, stu.dorm);
pd.setString(7, stu.tel);
updata = pd.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//关闭数据库
db.close(null);
}
return updata;
}
// 更新一行数据局
public int update(StuInfo stu){
String sql = "update StuInfo name=?,sex=?,age=?,groups=?,dorm=?,tel=? where stuid = ?";
pd = db.GetConn(sql);
try {
pd.setString(1, stu.stuid);
pd.setString(2, stu.name);
pd.setInt(3, stu.age);
pd.setString(4, stu.sex);
pd.setString(5, stu.groups);
pd.setString(6, stu.dorm);
pd.setString(7, stu.tel);
updata = pd.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//关闭数据库
db.close(null);
}
return updata;
}
//删除一行数据
public int del(StuInfo stu){
String sql = "delete from StuInfo where stuid= ?";
pd = db.GetConn(sql);
try {
pd.setString(1, stu.stuid);
updata = pd.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//关闭数据库
db.close(null);
}
return updata;
}
//查询一行数据或者全部 数据
/**
* @data 2018年4月5日19:41:53
* @param stu 数据库类
* @param boolean1 这是多行查询和单行 单行 false 全部 true
*/
public void Select(StuInfo stu,Boolean boolean1){
//结果集 并初始化
ResultSet st = null;
//查询一条
String sql="select * from StuInfo";
String sql2="select stuid,name,sex,age,groups,dorm,tel from StuInfo where stuid=?";
//判断查询多还是单
if(boolean1) pd = db.GetConn(sql);
else pd = db.GetConn(sql2);
try {
//判断查询多还是单
if(!boolean1) pd.setString(1, stu.stuid);
st = pd.executeQuery();
while (st.next()) {
stu.stuid = st.getString("stuid");
stu.name = st.getString("name");
stu.sex = st.getString("sex");
stu.age = st.getInt("age");
stu.dorm = st.getString("dorm");
stu.groups = st.getString("groups");
stu.tel = st.getString("tel");
stu.print();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//关闭数据库
db.close(st);
}
}
}
文章目录
  1. 1. date: 2018/4/8 1:33:12
  2. 2. 文章转载苏鹏
  • JDBC基础知识
    1. 1. 增删改查