java 对增删该查进行测试_java连接mysql增删改查测试通过
把數(shù)據(jù)庫連接作為一種方法調(diào)用實(shí)現(xiàn)增刪改查
源碼參考:
import java.sql.*;
public class JDBC_Test {
// 創(chuàng)建靜態(tài)全局變量
static Connection conn;
static Statement st;
public static void main(String[] args) {
insert(); //插入添加記錄
// update(); //更新記錄數(shù)據(jù)
// delete(); //刪除記錄
query(); //查詢記錄并顯示
}
/* 插入數(shù)據(jù)記錄,并輸出插入的數(shù)據(jù)記錄數(shù)*/
public static void insert() {
conn = getConnection(); // 首先要獲取連接,即連接到數(shù)據(jù)庫
try {
String sql = "INSERT INTO user(id,name,sex) VALUES (21,'dff2','Mf')"; // 插入數(shù)據(jù)的sql語句
st = (Statement) conn.createStatement(); // 創(chuàng)建用于執(zhí)行靜態(tài)sql語句的Statement對象
int count = st.executeUpdate(sql); // 執(zhí)行插入操作的sql語句,并返回插入數(shù)據(jù)的個(gè)數(shù)
System.out.println("向test表中插入 " + count + " 條數(shù)據(jù)"); //輸出插入操作的處理結(jié)果
conn.close(); //關(guān)閉數(shù)據(jù)庫連接
} catch (SQLException e) {
System.out.println("插入數(shù)據(jù)失敗" + e.getMessage());
}
}
/* 更新符合要求的記錄,并返回更新的記錄數(shù)目*/
public static void update() {
conn = getConnection(); //同樣先要獲取連接,即連接到數(shù)據(jù)庫
try {
String sql = "update user set id=32 where name = '565'";// 更新數(shù)據(jù)的sql語句
st = (Statement) conn.createStatement(); //創(chuàng)建用于執(zhí)行靜態(tài)sql語句的Statement對象,st屬局部變量
int count = st.executeUpdate(sql);// 執(zhí)行更新操作的sql語句,返回更新數(shù)據(jù)的個(gè)數(shù)
System.out.println("staff表中更新 " + count + " 條數(shù)據(jù)"); //輸出更新操作的處理結(jié)果
conn.close(); //關(guān)閉數(shù)據(jù)庫連接
} catch (SQLException e) {
System.out.println("更新數(shù)據(jù)失敗");
}
}
/* 查詢數(shù)據(jù)庫,輸出符合要求的記錄的情況*/
public static void query() {
conn = getConnection(); //同樣先要獲取連接,即連接到數(shù)據(jù)庫
try {
String sql = "select * from user"; // 查詢數(shù)據(jù)的sql語句
st = (Statement) conn.createStatement(); //創(chuàng)建用于執(zhí)行靜態(tài)sql語句的Statement對象,st屬局部變量
ResultSet rs = st.executeQuery(sql); //執(zhí)行sql查詢語句,返回查詢數(shù)據(jù)的結(jié)果集
System.out.println("最后的查詢結(jié)果為:");
while (rs.next()) { // 判斷是否還有下一個(gè)數(shù)據(jù)
// 根據(jù)字段名獲取相應(yīng)的值
int id = rs.getInt("id");
String name = rs.getString("name");
String sex = rs.getString("sex");
//輸出查到的記錄的各個(gè)字段的值
System.out.println(id+" "+name+" "+sex);
}
conn.close(); //關(guān)閉數(shù)據(jù)庫連接
} catch (SQLException e) {
System.out.println("查詢數(shù)據(jù)失敗");
}
}
/* 刪除符合要求的記錄,輸出情況*/
public static void delete() {
conn = getConnection(); //同樣先要獲取連接,即連接到數(shù)據(jù)庫
try {
String sql = "delete from user where name = 'LIYY'";// 刪除數(shù)據(jù)的sql語句
st = (Statement) conn.createStatement(); //創(chuàng)建用于執(zhí)行靜態(tài)sql語句的Statement對象,st屬局部變量
int count = st.executeUpdate(sql);// 執(zhí)行sql刪除語句,返回刪除數(shù)據(jù)的數(shù)量
System.out.println("staff表中刪除 " + count + " 條數(shù)據(jù)\n"); //輸出刪除操作的處理結(jié)果
conn.close(); //關(guān)閉數(shù)據(jù)庫連接
} catch (SQLException e) {
System.out.println("刪除數(shù)據(jù)失敗");
}
}
/* 獲取數(shù)據(jù)庫連接的函數(shù)*/
public static Connection getConnection() {
Connection con = null; //創(chuàng)建用于連接數(shù)據(jù)庫的Connection對象
try {
Class.forName("com.mysql.jdbc.Driver");// 加載Mysql數(shù)據(jù)驅(qū)動(dòng)
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "123456");// 創(chuàng)建數(shù)據(jù)連接
} catch (Exception e) {
System.out.println("數(shù)據(jù)庫連接失敗" + e.getMessage());
}
return con; //返回所建立的數(shù)據(jù)庫連接
}
}
總結(jié)
以上是生活随笔為你收集整理的java 对增删该查进行测试_java连接mysql增删改查测试通过的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 时间日期类JAVA包含地区属性_Java
- 下一篇: java求反字符_java 反取字符串