語句對象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;i6;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>