1 什么是jdbc
JDBC(Java DataBase Connectivity)就是Java数据库连接,说白了就是用Java语言来操作数据库。原来我们操作数据库是在控制台使用SQL语句来操作数据库,JDBC是用Java语言向数据库发送SQL语句。
2.连接步骤与方法
导jar包:驱动!
在项目下创建一个lib目录把jar包导入,
然后
步骤
-
加载驱动类:Class.forName(“类名”);
Class.forName("com.mysql.jdbc.Driver");
-
url、username、password的意义
-
localhost: 表示要连接的mysql数据库服务器的ip地址, 如果连接远程的mysql,需要写对方真实的ip, localhost表示连接本地的mysql服务器
-
3306: 表示mysql的端口
-
mydb: 表示需要连接mysql的那个数据库
-
useUnicode: 是否使用Unicode字符集,如果参数characterEncoding,设置为utf-8,本参数值必须设置为true, 默认值为false
-
characterEncoding: 当useUnicode设置为true时,指定字符编码。比如可设置为utf-8
-
useSSL: 是否进行SSL连接 高版本设置useSSL=true,不然会有警告信息
url: "jdbc:mysql://localhost:3306/" + "malldb?useUnicode=true&characterEncoding=utf8&useSSL=false", 用户名:"root" 密码:"root"
-
使用DriverManager类来得到Connection对象!
Connection对象的作用是连接数据库
DriverManager.getConnection("jdbc:mysql://localhost:3306/" +
"malldb?useUnicode=true&characterEncoding=utf8&useSSL=false",
"root","root");
-
-
使用Connection得到Statement对象
-
Statement是用来向数据库发送要执行的SQL语句的,
Statement stm = con.createStatement();
-
- 使用Statement对象对数据库进行增删改查操作
Statement对象调用executeUpdate(sql)方法
发送sql语句,并且执行sql语句
其中int类型的返回值表示执行这条SQL语句所影响的行数
可根据其判断是否修改数据库成功
String sql = “insert into user value(’zhangSan’, ’123’)”;
int m = stmt.executeUpdate(sql)
请注意,执行查询使用的不是executeUpdate()方法,而是executeQuery()方法。executeQuery()方法返回的是ResultSet,ResultSet封装了查询结果,我们称之为结果集。
String sql = “select * from user”;
ResultSet rs = stmt.executeQuery(sql);
public static void goodDelete(int id) throws ClassNotFoundException, SQLException {
//3.获取Statement对象
Connection con = getConnection();
Statement stm = con.createStatement();
String sql = "DELETE FROM `goods` WHERE `id` = " + id;
//4.发送sql语句,并且执行sql语句
int num = stm.executeUpdate(sql);
//5.处理结果
if(num > 0 ){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
//6.jdbc对象不会被垃圾回收器回收,释放内存资源
if(stm != null){
stm.close();
}
if(con != null){
con.close();
}
}
最后关闭资源
因为jdbc对象不会被垃圾回收器回收,所以要手动释放内存资源
if(stm != null){
stm.close();
}
if(con != null){
con.close();
}
3..PreparedStatement
PreparedStatement是什么
PreparedStatement叫预编译声明
PreparedStatement是Statement的子接口,你可以使用PreparedStatement来替换Statement。
PreparedStatement的好处:
-
防止SQL攻击;
public void login(String username, String password) {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
con = JdbcUtils.getConnection();
stmt = con.createStatement();
String sql = "SELECT * FROM user WHERE " +
"username='" + username +
"' and password='" + password + "'";
rs = stmt.executeQuery(sql);
if(rs.next()) {
System.out.println("欢迎" + rs.getString("username"));
} else {
System.out.println("用户名或密码错误!");
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
JdbcUtils.close(con, stmt, rs);
}
}
调用登陆方法login()
login("a' or '1'='1", "a' or '1'='1");
这会导致登录成功 ,而PreparedStatement使用参数化查询,将用户输入的数据作为参数传递,而不是将其直接拼接到SQL语句中,这样可以有效地防止SQL注入攻击。
-
提高代码的可读性,以可维护性;
在使用Connection创建PreparedStatement对象时需要给出一个SQL模板,所谓SQL模板就是有“?”的SQL语句,其中“?”就是参数。
String sql="select * from `tb_consuminfo` where `card_number`=? and month(`consume_date`)=?";
-
提高效率。
-
我们通过connection.preparedStatement(sql)方法来获得PreparedStatment对象,然后通过preStatement.setXXX来设置查询参数。
如果我有2个查询,SQL语句都一样,但是参数不同,使用PreparedStatment的话只需setXXX不同的参数、再次查询即可,而如果使用Statement的话,
需要重新创建Statement对象,connection.createStatement(sql)