数据库的增删改查

  这两天主要完成了一个关于javaweb连接数据库的增删改查(刚刚开始学,还望大神们指点)

主要用到了jsp(制作界面),和一些SQL语句,以及数据库的相关地方直接上代码:

①主界面:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html>
 4 <html>
 5 <head>
 6 <meta charset="UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 
10 <body>
11 <h5>通讯录系统</h5>
12 <br/>
13 <a href="add.jsp">添加信息</a>//引号中的是要跳转的页面以及要执行的操作
14 <br/>
15 <a href="delete.jsp">删除信息</a>
16 <br/>
17 <a href="change.jsp">修改信息</a>
18 <br/>
19 <a href="search.jsp">查看信息</a>
20 <br/>
21 <a href="look1.jsp">显示全部信息</a>
22 
23 </body>
24 </html>

主界面通过链接的方式与其他页面链接。

主界面完成后就是增删改查的各种功能和数据库的链接

②增

增加用户的界面

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2 pageEncoding="utf-8"%>
 3 <!DOCTYPE html>
 4 <html>
 5 <head>
 6 <meta charset="utf-8">
 7     <title>Insert title here</title>    
 8     </head>
 9     <body>
10     <h3>请输入要增加的信息</h3>
11     <form name="form1" method="post" action="add1.jsp">
12         姓名:<input name="name" type="text"/> <br>
13         年龄:<input name="age" type="text"/> <br>
14         电话:<input name="phone" type="text"/> <br>
15 <input type="submit" value="提交" />
16 <input type="reset" value="重置" />
17 <input type="button" name="Submit" onclick="javascript:history.back(-1);" value="返回上一页">
18 </form>
19 </body>
20 </html>

增加用户的操作包括连接数据库

 <%@ page language="java" contentType="text/html; charset=utf-8"
  pageEncoding="utf-8" import="java.sql.*"%>
  <!DOCTYPE html>
 <html>
  <head>
  <meta charset="utf-8">
  <title>Insert title here</title>
  </head>
  <body>
 <%
 response.setContentType("text/html;charset=UTF-8");
 request.setCharacterEncoding("UTF-8"); 
 
 //MySQL 8.0 以下版本 - JDBC 驱动名及数据库 URL
 //String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
 //String DB_URL = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
 String biaoming ="list";
 // MySQL 8.0 以上版本 - JDBC 驱动名及数据库 URL
  String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  String DB_URL = "jdbc:mysql://localhost:3306/hero?characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";//数据库名记得改啊在这
 // 数据库的用户名与密码,需要根据自己的设置
 final String USER = "root";
 final String PASS = "jiang123456";
 // 注册 JDBC 驱动
 Class.forName(JDBC_DRIVER);
 
 int count=0;//获取结果集的长度
 Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);
 Statement stmt = conn.createStatement();//到这里连接数据库的操作就结束了死东西知道就行套模板.后面有很多重复的地方可以考虑将上面这部分改为一个函数令创一个class.
 ResultSet set=null;//结果集
 try {
     set=stmt.executeQuery("select * from list");//用结果集set来存储表里的内容        
    } catch (SQLException e) 
 {
        // TODO Auto-generated catch block
         e.printStackTrace();
 }
 String x1 = request.getParameter("name");//获取上一个界面输入的姓名
 String x2 = request.getParameter("age");
 String x3 = request.getParameter("phone");
 String name1[]=new String[100]; //建立一个字符串数组用来存表中已经存在的name
 int index=-1;
int i=0;
 while(set.next())//这一部分则是看输入结果是否已经存在
     { 
         name1[i]=set.getString("name");
         i++;
         count++;
     }
     for(int j=0;j<count;j++)
     {
         if(x1.equals(name1[j]))
         {
             out.print("输入数据重复请重新输入");
             index=-1;break;
         }
         else
         {
             index=1;
         }
     }
     if(index==1)
    {
         xieru(stmt,set,x1,x2,x3);    
     }
     
     stmt.close();//全部操作结束后要关闭驱动死东西可套用
     conn.close();
 %>
 <%!//方法要写这里上面会报错emm
 
 public static void xieru(Statement stmt,ResultSet set,String x1,String x2,String x3) 
 {
     
     //System.out.println("INSERT INTO kechengbiao (ke,jiao,di)VALUES('"+x1+"',"+"'"+x2+"',"+"'"+x3+"'"+");");
     try {
         set=stmt.executeQuery("select *from list");        
         stmt.executeUpdate("INSERT INTO list (name,age,phone)values (\""+x1+"\","+"\""+x2+"\","+"\""+x3+"\""+");");    
         } catch (SQLException e) 
     {
             // TODO Auto-generated catch block
            e.printStackTrace();
     }
 } 
 
 
%>
 <input type="button" name="Submit" onclick="javascript:history.back(-1);" value="返回上一页">
 </body>
 </html>

上面这俩步骤就是增加时的代码。后面的基本都是按这个套路来的,

③改

修改数据的界面

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
    <title>Insert title here</title>    
    </head>
    <body>
    <h3>请输入要更改的人员信息</h3>
    <form name="form3" method="post" action="change1.jsp">
        姓名:<input name="name" type="text"/> <br>
        修改年龄:<input name="age" type="text"/> <br>
        修改电话:<input name="phone" type="text"/> <br>
<input type="submit" value="提交" />
<input type="reset" value="重置" />
</form>
</body>
</html>

修改数据的的操作,我这块是根据输入的名字,来修改年龄以及电话号码的。要有其他自己记得看看啊

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8"); 

//MySQL 8.0 以下版本 - JDBC 驱动名及数据库 URL
//String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
//String DB_URL = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
String biaoming ="list";
// MySQL 8.0 以上版本 - JDBC 驱动名及数据库 URL
 String JDBC_DRIVER = "com.mysql.jdbc.Driver";
 String DB_URL = "jdbc:mysql://localhost:3306/hero?characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
// 数据库的用户名与密码,需要根据自己的设置
final String USER = "root";
final String PASS = "jiang123456";
// 注册 JDBC 驱动
Class.forName(JDBC_DRIVER);

Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);
Statement stmt = conn.createStatement();
String x1 = request.getParameter("name");
String x2 = request.getParameter("age");
String x3 = request.getParameter("phone");
change(stmt,x1,x2,x3);
stmt.close();
conn.close();
%>
<%!

public static void change(Statement stmt,String x1,String x2,String x3) 
{
    
    //System.out.println("INSERT INTO kechengbiao (ke,jiao,di)VALUES('"+x1+"',"+"'"+x2+"',"+"'"+x3+"'"+");");
    try {
            stmt.executeUpdate("update list set age=\'"+x2+"\',phone=\'"+x3+"\' where name=\'"+x1+"\'");
//这一块还是建议多了解一下1SQL语句的使用写的时候花了好长时间,转义字符一定要好好看好好学。 }
catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } %> </body> </html>

④删除的主界面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>请输入要删除的人员信息</h3>
<form name="form2" method="post" action="delete1.jsp">
姓名:<input name="name" type="text"/> <br>
<input type="submit" value="提交" />
<input type="reset" value="重置" />
</body>
</html>

删除的代码操作

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%!
public static void delete(Statement stmt,String x1) 
{
    
    //System.out.println("INSERT INTO kechengbiao (ke,jiao,di)VALUES('"+x1+"',"+"'"+x2+"',"+"'"+x3+"'"+");");
            try{stmt.executeUpdate("delete from list where name="+"\'"+x1+"\'");
} catch (SQLException e) 
            {
    // TODO Auto-generated catch block
    e.printStackTrace();
}    }
%>
<%
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8"); 

//MySQL 8.0 以下版本 - JDBC 驱动名及数据库 URL
//String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
//String DB_URL = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
String biaoming ="list";
// MySQL 8.0 以上版本 - JDBC 驱动名及数据库 URL
 String JDBC_DRIVER = "com.mysql.jdbc.Driver";
 String DB_URL = "jdbc:mysql://localhost:3306/hero?characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
// 数据库的用户名与密码,需要根据自己的设置
final String USER = "root";
final String PASS = "jiang123456";
// 注册 JDBC 驱动
Class.forName(JDBC_DRIVER);

Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);
Statement stmt = conn.createStatement();
String x1 = request.getParameter("name");

delete(stmt,x1);
stmt.close();
conn.close();
%>


</body>
</html>

⑤显示全部数据主界面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>是否查看全部人员信息</h3>
<form name="form3" method="post" action="look1.jsp">
<input name="name" type="text"/> <br>
<input type="submit" value="提交" />
<input type="reset" value="重置" />
</body>
</html>

显示全部数据主代码

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8"); 
ResultSet set=null;
//MySQL 8.0 以下版本 - JDBC 驱动名及数据库 URL
//String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
//String DB_URL = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
String biaoming ="list";
// MySQL 8.0 以上版本 - JDBC 驱动名及数据库 URL
 String JDBC_DRIVER = "com.mysql.jdbc.Driver";
 String DB_URL = "jdbc:mysql://localhost:3306/hero?characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
// 数据库的用户名与密码,需要根据自己的设置
final String USER = "root";
final String PASS = "jiang123456";
// 注册 JDBC 驱动
Class.forName(JDBC_DRIVER);

Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);
Statement stmt = conn.createStatement();
String x1 = request.getParameter("name");
try{
    set=stmt.executeQuery("select *from list");   //这块就是利用SQL语句返回一个结果集然后再遍历所有数据 
}
catch (SQLException e) 
{
// TODO Auto-generated catch block
e.printStackTrace();
}    
while(set.next())
{
out.print("姓名:"+set.getString("name")+" 年龄:"+set.getString("age")+"电话号码:"+set.getString("phone")+"<br>");
}
stmt.close();
conn.close();
%>


</body>
</html>

⑥查询人员信息主界面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>请输入要查询的人员信息</h3>
<form name="form5" method="post" action="search1.jsp">
姓名:<input name="name" type="text"/> <br>
<input type="submit" value="提交" />
<input type="reset" value="重置" />
</body>
</html>

主要代码

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8"); 
ResultSet set=null;
//MySQL 8.0 以下版本 - JDBC 驱动名及数据库 URL
//String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
//String DB_URL = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
String biaoming ="list";
// MySQL 8.0 以上版本 - JDBC 驱动名及数据库 URL
 String JDBC_DRIVER = "com.mysql.jdbc.Driver";
 String DB_URL = "jdbc:mysql://localhost:3306/hero?characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
// 数据库的用户名与密码,需要根据自己的设置
final String USER = "root";
final String PASS = "jiang123456";
// 注册 JDBC 驱动
Class.forName(JDBC_DRIVER);

Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);
    Statement stmt = conn.createStatement();
    String x1 = request.getParameter("name");
    try{
        set=stmt.executeQuery("select *from list");    
    }
    catch (SQLException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    int y=1;
    while(set.next())
    {
        String y1=set.getString("name");
        if(y1.equals(x1))
        {    
            out.print("姓名:"+set.getString("name")+" 年龄:"+set.getString("age")+"电话号码:"+set.getString("phone")+"<br>");
            y=-1;
        break;
    }
    else
    {
        continue;
    }
}
if(y==1)
{
    out.print("查找人员不存在");
}
stmt.close();
conn.close();
%>


</body>
</html>

以上就是所有代码了,建议下载SQLyog实例化,方便直接查看数据库的变化。东西看的很多其实大部分都是相似的,以后继续完善。上面全是建立的jsp文件别建错了。

 

 

 

posted @ 2021-10-26 22:23  O_O似曾相识  阅读(222)  评论(0编辑  收藏  举报