欢迎来到代码驿站!

JSP代码

当前位置:首页 > 软件编程 > JSP代码

详解JSP中的语句对象Statement操作MySQL的使用实例

时间:2021-09-01 09:01:36|栏目:JSP代码|点击:

语句对象Statement包含两个主要方法:executeUpdate()方法执行数据的更新操作(添加记录,删除记录,更新记录),executeQuery()方法用来执行数据的查询操作(查询记录)

添加记录

<%@page language="java" contentType="text/html;charset=gb2312"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
  <head>
    <title>添加用户记录</title>
  </head>
  <body>
    <%
      String url = "jdbc:mysql://localhost:3306/javaweb";//连接数据库的url地址
      String user = "root";//登录数据库的用户名
      String password = "zhangda890126;;";//登录数据库的用户名的密码
      Connection conn = null;
      try{
        Class.forName("com.mysql.jdbc.Driver");//加载JDBC驱动程序
        conn = DriverManager.getConnection(url,user,password);//链接数据库
         
      }catch(ClassNotFoundException e){
        out.println("找不到驱动类");//抛出异常时,提示信息
      }catch(SQLException e){
        out.println("链接MySQL数据库失败");//处理SQLException异常
      }
       
      try{
        //创建语句对象Statement
        Statement stmt = conn.createStatement();
         
        String adduser = "INSERT INTO user(userid,username,password) VALUES (null,'James','1234')";//添加用户
         
        stmt.executeUpdate(adduser);//执行语句
      }catch(SQLException e){
        out.println("添加用户信息失败");
      }
    %>
  </body>
</html>

<html>
  <head>
    <title>添加多个用户记录</title>
  </head>
  <body>
    <%
      String url = "jdbc:mysql://localhost:3306/javaweb";//连接数据库的url地址
      String user = "root";//登录数据库的用户名
      String password = "zhangda890126;;";//登录数据库的用户名的密码
      Connection conn = null;
      try{
        Class.forName("com.mysql.jdbc.Driver");//加载JDBC驱动程序
        conn = DriverManager.getConnection(url,user,password);//链接数据库
      }catch(ClassNotFoundException e){
        out.println("找不到驱动类");//抛出异常时,提示信息
      }catch(SQLException e){
        out.println("链接MySQL数据库失败");//处理SQLException异常
      }
      try{
        //创建语句对象Statement
        Statement stmt = conn.createStatement();
        //删除userid为1的用户信息
        for(int i=2;i<6;i++){
          String username = "zhangda_"+i;
          String adduser = "INSERT INTO user (userid,username,password) VALUES (null,'"+username+"','1234')";//添加用户
          stmt.executeUpdate(adduser);//执行语句
        }
      }catch(SQLException e){
        out.println("添加用户信息失败");
      }
    %>
  </body>
</html>

更新记录

<%@page language="java" contentType="text/html;charset=gb2312"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
  <head>
    <title>添加用户记录</title>
  </head>
  <body>
    <%
      String url = "jdbc:mysql://localhost:3306/javaweb";//连接数据库的url地址
      String user = "root";//登录数据库的用户名
      String password = "zhangda890126;;";//登录数据库的用户名的密码
      Connection conn = null;
      try{
        Class.forName("com.mysql.jdbc.Driver");//加载JDBC驱动程序
        conn = DriverManager.getConnection(url,user,password);//链接数据库
         
      }catch(ClassNotFoundException e){
        out.println("找不到驱动类");//抛出异常时,提示信息
      }catch(SQLException e){
        out.println("链接MySQL数据库失败");//处理SQLException异常
      }
       
      try{
        //创建语句对象Statement
        Statement stmt = conn.createStatement();
        //更新userid为1的用户信息,更新其密码为12345
        String updateuser = "UPDATE user SET password='12345' WHERE userid=1;";//添加用户
         
        stmt.executeUpdate(updateuser);//执行语句
      }catch(SQLException e){
        out.println("更新用户信息失败");
      }
    %>
  </body>
</html>

删除记录

<%@page language="java" contentType="text/html;charset=gb2312"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
  <head>
    <title>添加用户记录</title>
  </head>
  <body>
    <%
      String url = "jdbc:mysql://localhost:3306/javaweb";//连接数据库的url地址
      String user = "root";//登录数据库的用户名
      String password = "zhangda890126;;";//登录数据库的用户名的密码
      Connection conn = null;
      try{
        Class.forName("com.mysql.jdbc.Driver");//加载JDBC驱动程序
        conn = DriverManager.getConnection(url,user,password);//链接数据库
         
      }catch(ClassNotFoundException e){
        out.println("找不到驱动类");//抛出异常时,提示信息
      }catch(SQLException e){
        out.println("链接MySQL数据库失败");//处理SQLException异常
      }
       
      try{
        //创建语句对象Statement
        Statement stmt = conn.createStatement();
        //删除userid为1的用户信息
        String deleteuser = "DELETE FROM user WHERE userid=1;";//添加用户
         
        stmt.executeUpdate(deleteuser);//执行语句
      }catch(SQLException e){
        out.println("删除用户信息失败");
      }
    %>
  </body>
</html>

上一篇:java tapestry5 布局 参数的处理

栏    目:JSP代码

下一篇:在JSP页面中动态生成图片验证码的方法实例

本文标题:详解JSP中的语句对象Statement操作MySQL的使用实例

本文地址:http://www.codeinn.net/misctech/171731.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有