无为清净楼资源网 Design By www.qnjia.com
语句对象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>
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年09月20日
2024年09月20日
- 青稞荞麦《破茧》新生代组合[WAV+CUE]
- 因系统升级后出现BUG 《FF16》PS5完全版宣布延期
- 《黑神话:悟空》周伯通!大佬用左右手分别挑战两只虎先锋
- 《黑神话:悟空》攻击动作致敬《大话西游》:这也太帅了!
- 《星舰铳犬太阳系物语》游戏背景介绍
- 《战锤40K星际战士2》终极版有后续DLC说明
- 《第9层》游戏特色玩法介绍
- 刀郎.2006-刀郎【九雨天下】【WAV+CUE】
- 59.9高校生.1994-素兰万岁【上华】【WAV+CUE】
- ECHO.1991-你是爱我还是需要我(国专)【EMI百代】【WAV+CUE】
- 交错战线零课主线困难通关阵容推荐一览
- 交错战线挖掘矿场角色推荐攻略
- 命运圣契公测平民阵容怎么搭配 新手开荒平民阵容攻略
- 金梅《现代发烧的声音2》发烧碟[WAV+CUE]
- 雨果唱片-《历史录音珍藏系列-现代芭蕾舞剧·红色娘子军》2CD【WAV】