詳解JSP中的語句對象Statement操作MySQL的使用實例
更新時間:2016年04月14日 16:29:03 作者:張大鵬
這篇文章主要介紹了JSP中的語句對象Statement操作MySQL的使用實例,講解了添加記錄、更新記錄、刪除記錄三種操作,說明都在代碼注釋中簡單明了,需要的朋友可以參考下
語句對象Statement包含兩個主要方法:executeUpdate()方法執(zhí)行數(shù)據(jù)的更新操作(添加記錄,刪除記錄,更新記錄),executeQuery()方法用來執(zhí)行數(shù)據(jù)的查詢操作(查詢記錄)
添加記錄
<%@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";//連接數(shù)據(jù)庫的url地址 String user = "root";//登錄數(shù)據(jù)庫的用戶名 String password = "zhangda890126;;";//登錄數(shù)據(jù)庫的用戶名的密碼 Connection conn = null; try{ Class.forName("com.mysql.jdbc.Driver");//加載JDBC驅(qū)動程序 conn = DriverManager.getConnection(url,user,password);//鏈接數(shù)據(jù)庫 }catch(ClassNotFoundException e){ out.println("找不到驅(qū)動類");//拋出異常時,提示信息 }catch(SQLException e){ out.println("鏈接MySQL數(shù)據(jù)庫失敗");//處理SQLException異常 } try{ //創(chuàng)建語句對象Statement Statement stmt = conn.createStatement(); String adduser = "INSERT INTO user(userid,username,password) VALUES (null,'James','1234')";//添加用戶 stmt.executeUpdate(adduser);//執(zhí)行語句 }catch(SQLException e){ out.println("添加用戶信息失敗"); } %> </body> </html> <html> <head> <title>添加多個用戶記錄</title> </head> <body> <% String url = "jdbc:mysql://localhost:3306/javaweb";//連接數(shù)據(jù)庫的url地址 String user = "root";//登錄數(shù)據(jù)庫的用戶名 String password = "zhangda890126;;";//登錄數(shù)據(jù)庫的用戶名的密碼 Connection conn = null; try{ Class.forName("com.mysql.jdbc.Driver");//加載JDBC驅(qū)動程序 conn = DriverManager.getConnection(url,user,password);//鏈接數(shù)據(jù)庫 }catch(ClassNotFoundException e){ out.println("找不到驅(qū)動類");//拋出異常時,提示信息 }catch(SQLException e){ out.println("鏈接MySQL數(shù)據(jù)庫失敗");//處理SQLException異常 } try{ //創(chuàng)建語句對象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);//執(zhí)行語句 } }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";//連接數(shù)據(jù)庫的url地址 String user = "root";//登錄數(shù)據(jù)庫的用戶名 String password = "zhangda890126;;";//登錄數(shù)據(jù)庫的用戶名的密碼 Connection conn = null; try{ Class.forName("com.mysql.jdbc.Driver");//加載JDBC驅(qū)動程序 conn = DriverManager.getConnection(url,user,password);//鏈接數(shù)據(jù)庫 }catch(ClassNotFoundException e){ out.println("找不到驅(qū)動類");//拋出異常時,提示信息 }catch(SQLException e){ out.println("鏈接MySQL數(shù)據(jù)庫失敗");//處理SQLException異常 } try{ //創(chuàng)建語句對象Statement Statement stmt = conn.createStatement(); //更新userid為1的用戶信息,更新其密碼為12345 String updateuser = "UPDATE user SET password='12345' WHERE userid=1;";//添加用戶 stmt.executeUpdate(updateuser);//執(zhí)行語句 }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";//連接數(shù)據(jù)庫的url地址 String user = "root";//登錄數(shù)據(jù)庫的用戶名 String password = "zhangda890126;;";//登錄數(shù)據(jù)庫的用戶名的密碼 Connection conn = null; try{ Class.forName("com.mysql.jdbc.Driver");//加載JDBC驅(qū)動程序 conn = DriverManager.getConnection(url,user,password);//鏈接數(shù)據(jù)庫 }catch(ClassNotFoundException e){ out.println("找不到驅(qū)動類");//拋出異常時,提示信息 }catch(SQLException e){ out.println("鏈接MySQL數(shù)據(jù)庫失敗");//處理SQLException異常 } try{ //創(chuàng)建語句對象Statement Statement stmt = conn.createStatement(); //刪除userid為1的用戶信息 String deleteuser = "DELETE FROM user WHERE userid=1;";//添加用戶 stmt.executeUpdate(deleteuser);//執(zhí)行語句 }catch(SQLException e){ out.println("刪除用戶信息失敗"); } %> </body> </html>
您可能感興趣的文章:
- MySQL 將文件導(dǎo)入數(shù)據(jù)庫(load data Statement)
- MySQL?如何將查詢結(jié)果導(dǎo)出到文件(select?…?into?Statement)
- MySQL?Prepared?Statement?預(yù)處理的操作方法
- MySQL?binlog格式之Row和Statement語句詳解
- The MySQL server is running with the --read-only option so it cannot execute this statement
- MySQL:Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEM
- 解決mysql報錯You must reset your password using ALTER USER statement before executing this statement問題
相關(guān)文章
jsp中將后臺傳遞過來的json格式的list數(shù)據(jù)綁定到下拉菜單select
后臺傳遞過來的json格式的list數(shù)據(jù)如何綁定到下拉菜單,下面有個不錯的示例,感興趣的朋友可以參考下2013-10-10Apache+Servlet+Jsp環(huán)境設(shè)置(中)
Apache+Servlet+Jsp環(huán)境設(shè)置(中)...2006-10-10JavaScript實現(xiàn)圖片倒影效果 - reflex.js
本篇文章主要介紹了JavaScript實現(xiàn)圖片倒影效果,在實際中很實用,有需要的可以了解一下。2016-10-10