利用JDBC实现数据库的操作
程序员文章站
2022-06-26 08:08:03
【实训目的】(1)掌握利用JDBC实现数据库的添加操作(2)掌握利用JDBC实现数据库的更新操作(3)利用JDBC实现数据库的查询操作(4)利用JDBC实现数据库的删除操作【实训要求】(1)创建insert.jsp页面(2)创建update.jsp(3)创建query.jsp(4)创建delete.jsp1、insert.jsp<%@ page import="java.sql.Connection" %><%@ page import="java.sql.Stat...
【实训目的】
(1)掌握利用JDBC实现数据库的添加操作
(2)掌握利用JDBC实现数据库的更新操作
(3)利用JDBC实现数据库的查询操作
(4)利用JDBC实现数据库的删除操作
【实训要求】
(1)创建insert.jsp页面
(2)创建update.jsp
(3)创建query.jsp
(4)创建delete.jsp
1、insert.jsp
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.SQLException" %><%--
Created by IntelliJ IDEA.
User: 12526
Date: 2021/1/13
Time: 17:12
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>插入记录实例</title>
</head>
<body>
<%
Connection con = null;
Statement stmt = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3308/数据库名称","用户名","密码");
stmt = con.createStatement();
String sql = "insert into users(id,name,password) values(5,'hehe','asdf')";
stmt.execute(sql);
}
catch (ClassNotFoundException ex){
ex.printStackTrace();
}
catch (SQLException ex1){
ex1.printStackTrace();
}
finally {
try{
if(stmt!=null){
stmt.close();
stmt = null;
}
if(con!=null){
con.close();
con =null;
}
}
catch (SQLException ex){
ex.printStackTrace();
}
}
%>
</body>
</html>
可以看到和直接使用insert语句是能达到同样的效果的,不过使用insert语句的时候,要确保已经在数据库中创建了一个叫做users的表格,以及已经有创建好了相应的列名和字段
在浏览器里面输入对应的映射路径即可,由于程序编写的原因,网页每刷新一次,都会往数据库中插入一条数据。
如果是用idea创建javaweb项目的话,要现在idea里面建立数据库的连接,否则是连不上数据库的。
(2)update.jsp
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.SQLException" %><%--
Created by IntelliJ IDEA.
User: 12526
Date: 2021/1/13
Time: 17:45
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>更新记录实例</title>
</head>
<body>
<h3>更新记录实例</h3>
<%
Connection con = null;
Statement stmt = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3308/manage","root","yww1352465324");
stmt = con.createStatement();
String sql = "update users set name = 'hehe' where id = 6";
stmt.executeUpdate(sql);
out.println("<p>成功更新记录<p>");
}
catch (ClassNotFoundException ex){
ex.printStackTrace();
}
catch (SQLException ex1){
ex1.printStackTrace();
}
finally {
try{
if(stmt!=null){
stmt.close();
stmt = null;
}
if(con!=null){
con.close();
con =null;
}
}
catch (SQLException ex){
ex.printStackTrace();
}
}
%>
</body>
</html>
数据库原先的情况:
执行jsp代码后的情况
页面情况:
(3)query.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>查询实例</title>
</head>
<body>
<%
//此进行连接数据库
String url="jdbc:mysql://127.0.0.1:3308/manage"; //manage为数据库名称
String dbuser="xxx"; //数据库账户
String dbpwd="xxx"; //数据库密码
try
{
Class.forName("com.mysql.jdbc.Driver"); //加载驱动 JspStudy
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
//取得数据库连接conn
Connection conn=DriverManager.getConnection(url, dbuser, dbpwd);
PreparedStatement ps=null;
ResultSet rs=null;
//声明数据库字段
String id="";
String name="";
String password="";
try
{
String sql="select * from users";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next())
{
id=rs.getString(1);
name=rs.getString(2);
password=rs.getString(3);
out.println("ID:"+id+"<br>");
out.println(name+"<br><br>");
out.println(password+"<br><br>");
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if(rs!=null)
rs.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if(ps!=null)
ps.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if(conn!=null)
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
%>
</body>
</html>
(4)delete.jsp
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.SQLException" %><%--
Created by IntelliJ IDEA.
User: 12526
Date: 2021/1/14
Time: 13:38
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>删除实例</title>
</head>
<body>
<%
Connection con = null;
Statement stmt =null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3308/manage","xxx(账户)","xxxxxxxx(密码)");
stmt = con.createStatement();
String sql = "delete from users where id = 6";
stmt.execute(sql);
}catch (ClassNotFoundException ex){
ex.printStackTrace();
}catch (SQLException ex){
ex.printStackTrace();
}finally {
try{
if(stmt!=null){
stmt.close();
stmt = null;
}
if(con!=null){
con.close();
con = null;
}
}catch (SQLException ex){
ex.printStackTrace();
}
}
%>
</body>
</html>
数据库原先内容:
执行delete.jsp后的数据库内容
以上就是利用JDBC实现数据库操作的相关基本方法
本文地址:https://blog.csdn.net/weixin_45394002/article/details/112576093
上一篇: sql语句实例:回购率、复购率怎么理解?
下一篇: 这么优雅的Java ORM没见过吧!