目录:
1.JDBC简介
2.Statement 和 PreparedStatement 的区别
参数化查询 / 动态查询 :
Statement 和 PreparedStatement 都能对数据库进行增、删、改、查,但两者又有所不同。PreparedStatement支持参数化查询,可避免SQL注入攻击。PreparedStatement可以将参数作为占位符传递到SQL查询中,然后通过setXXX()将参数值绑定到占位符上,这是PreparedStatement的参数化查询 / 动态查询。(先有占位符,后通过setXXX( )方法将参数与占位符替换,成功填入值)
Statement不支持参数化查询,如果你需要动态添加数据 / 动态查询,必须使用拼接字符串的方式来创建SQL语句,容易导致SQL注入攻击。
性能 :
一般来说 PreparedStatement的性能优于Statement。
因为PreparedStatement的sql语句在执行前已经编译过,而Statement的SQL语句每次执行都需要编译,所以性能比较差。代码可读性 :
由于PreparedStatement支持参数化参数,使的代码更加清晰和易于维护,也使代码更加模块化、易于理解。而Statement的SQL语句中混杂着数据,代码可读性较差。
3.导入连接数据库Jar包
在JSP中连接数据库时除了要导入 mysql–connector-java.jar 包, 一般还要导入关于JSP的
jsp–api.jar包。
4.JDBC操作
4.1 Statement操作
使用Statement“添加数据”
<%@ page import="java.sql.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>JSP中通过Statement"添加数据"</title>
</head>
<body>
<%
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/a3?useUnicode=true&characterEncoding=UTF-8";
//2,获得连接数据库的Connection对象
Connection conn = DriverManager.getConnection(url,"root", "root");
//3.获得操作数据库的Statement对象
Statement stat = conn.createStatement();
//sql语句
String sql = "insert into book(bno,bname,author,publishment) values('1','JavaWeb开发','zhangsan','北京出版社')";
//4.执行该语句
int i = stat.executeUpdate(sql);
out.print("成功添加"+i+"行");
//5.关闭资源
stat.close();
conn.close();
%>
</body>
</html>
使用Statement“删除数据”
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>JSP中通过Statement"删除数据"</title>
</head>
<body>
<%
//1.通过反射加载驱动
Class.forName("com.mysql.jdbc.Driver");
//url
String url = "jdbc:mysql://localhost:3306/a3?useUnicode=true&characterEncoding=UTF-8";
//2.通过connection对象连接数据库
Connection conn = DriverManager.getConnection(url, "root", "root");
//3.通过Statement对象操作数据库
Statement stat = conn.createStatement();
String sql = "delete from book where bno ='1'";
//4.调用方法操作数据库
int i = stat.executeUpdate(sql);
out.print("成功删除"+i+"行数据");
//关闭资源
stat.close();
conn.close();
%>
</body>
</html>
使用Statement“修改数据”
<%@ page import="com.mysql.jdbc.Driver" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>JSP中通过Statement"修改数据"</title>
</head>
<body>
<%
//1.通过反射加载驱动
Class.forName("com.mysql.jdbc.Driver");
//url
String url = "jdbc:mysql://localhost:3306/a3?useUnicode=true&characterEncoding=UTF-8";
//2.通过connection对象连接数据库
Connection conn = DriverManager.getConnection(url, "root", "root");
//3.通过Statement对象操作数据库
Statement stat = conn.createStatement();
String sql = "update book set bname = 'JavaJavaJava' where bno = '1'";
//4.调用方法操作数据库
int i = stat.executeUpdate(sql);
out.print("成功修改"+i+"行数据");
//关闭资源
stat.close();
conn.close();
%>
</body>
</html>
使用Statement“查询数据”
<%@ page import="com.mysql.jdbc.Driver" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>JSP中通过Statement"查询数据"</title>
</head>
<body>
<%
//1.通过反射加载驱动
Class.forName("com.mysql.jdbc.Driver");
//url
String url = "jdbc:mysql://localhost:3306/a3?useUnicode=true&characterEncoding=UTF-8";
//2.通过connection对象连接数据库
Connection conn = DriverManager.getConnection(url, "root", "root");
//3.通过Statement对象操作数据库
Statement stat = conn.createStatement(); //Statement : 不能参数化查询
String sql = "select * from book where bno = 1";
//4.调用方法操作数据库
ResultSet rs = stat.executeQuery(sql);
while (rs.next()) {
out.print(rs.getString(1));
out.print(rs.getString(2));
out.print(rs.getString(3));
out.print(rs.getString(4));
}
//关闭资源
stat.close();
conn.close();
%>
</body>
</html>
4.2 PreparedStatement操作
使用PreparedStatement“添加数据”
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>add1.jsp</title>
</head>
<body>
<%
//1.通过反射连接驱动
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/a3?useUnicode=true&characterEncoding=UTF-8";
//2.获得连接数据库的Connection对象
Connection conn = DriverManager.getConnection(url, "root", "root");
//3.获得操作数据的PreparedStatement对象 (PreparedStatement对象可进行参数化设置)
String sql = "insert into book values(?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
//设置参数(补充占位符)
ps.setString(1,"111");
ps.setString(2,"诗词鉴赏");
ps.setString(3,"俞平伯");
ps.setString(4,"陕西师范大学出版社");
int i = ps.executeUpdate();
out.print("成功添加"+i+"行数据!");
%>
</body>
</html>
使用PreparedStatement“删除数据”
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>delete1.jsp</title>
</head>
<body>
<%
//1.通过反射连接驱动
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/a3?useUnicode=true&characterEncoding=UTF-8";
//2.获得连接数据库的Connection对象
Connection conn = DriverManager.getConnection(url, "root", "root");
//3.获得操作数据的PreparedStatement对象 (PreparedStatement对象可进行参数化设置)
String sql = "delete from book where bno = ?";
PreparedStatement ps = conn.prepareStatement(sql);
//设置参数(补充占位符)
ps.setString(1,"111");
//4.执行sql操作
int i = ps.executeUpdate();
out.print("成功删除"+i+"行数据!");
%>
</body>
</html>
使用PreparedStatement“修改数据”
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>update1.jsp</title>
</head>
<body>
<%
//1.通过反射连接驱动
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/a3?useUnicode=true&characterEncoding=UTF-8";
//2.获得连接数据库的Connection对象
Connection conn = DriverManager.getConnection(url, "root", "root");
//3.获得操作数据的PreparedStatement对象 (PreparedStatement对象可进行参数化设置)
String sql = "update book set bname = ? where bno = ?";
PreparedStatement ps = conn.prepareStatement(sql);
//设置参数(补充占位符)
ps.setString(1,"JavaWeb");
ps.setString(2,"112233");
//4.执行sql语句
int i = ps.executeUpdate();
out.print("成功修改"+i+"行数据!");
%>
</body>
</html>
使用PreparedStatement“查询数据”
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>select1.jsp</title>
</head>
<body>
<%
//1.通过反射连接驱动
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/a3?useUnicode=true&characterEncoding=UTF-8";
//2.获得连接数据库的Connection对象
Connection conn = DriverManager.getConnection(url, "root", "root");
//3.获得操作数据的PreparedStatement对象 (PreparedStatement对象可进行参数化设置)
String sql = "select * from book where bno = ?";
PreparedStatement ps = conn.prepareStatement(sql);
//设置参数(补充占位符)
ps.setString(1,"0009999112");
//4.获得结果集对象
ResultSet rs = ps.executeQuery(); //返回值为结果集对象
//5.解析结果集对象
while (rs.next()) { //如果有数据
String bno = rs.getString(1);
String bname = rs.getString(2);
String author = rs.getString(3);
String publishment = rs.getString(4);
out.print(bno+" "+bname+" "+author+" "+publishment);
}
%>
</body>
</html>
原文地址:https://blog.csdn.net/m0_70720417/article/details/134700729
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_8047.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!