package com.atguigu.api.preparedstatement;标签:preparedStatement,java,resultSet,改查,System,connection,增删,id,out From: https://www.cnblogs.com/zhazhawei906/p/17119764.html
import org.junit.Test;
import java.sql.*;
import java.util.Scanner;
public class PreparedStatement_CRUD_Test {
//TODO java实现数据库添加功能
@Test
public void InsertTest() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1/atguigu", "root", "123456");
String insertSql = "insert into t_user(account,password,nickname)values(?,?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(insertSql);
preparedStatement.setObject(1,"lisi");
preparedStatement.setObject(2,"lisi");
preparedStatement.setObject(3,"李四");
int i = preparedStatement.executeUpdate();
if (i>0){
System.out.println("数据插入成功!");
}else{
System.out.println("数据插入失败!");
}
preparedStatement.close();
connection.close();
}
//TODO java实现数据库修改功能
@Test
public void UpdateTest() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql:///atguigu", "root", "123456");
String updateSql = "update t_user set nickname = ? where id = ?";
PreparedStatement preparedStatement = connection.prepareStatement(updateSql);
preparedStatement.setObject(1,"王五");
preparedStatement.setObject(2,3);
int i = preparedStatement.executeUpdate();
if (i>=1){
System.out.println("数据修改成功!");
}else {
System.out.println("数据修改失败!");
}
}
//TODO java实现数据库删除功能
@Test
public void DeleteTest() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/atguigu", "root", "123456");
String deleteSql = "delete from t_user where id = ?";
PreparedStatement preparedStatement = connection.prepareStatement(deleteSql);
preparedStatement.setObject(1,5);
int i = preparedStatement.executeUpdate();
if (i>=1){
System.out.println("数据删除成功!");
}else {
System.out.println("数据删除失败!");
}
preparedStatement.close();
connection.close();
}
//TODO java实现数据库查询功能
@Test
public void SelectTest() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/atguigu", "root", "123456");
String selectSql = "select * from t_user";
String selectSql_id = "select * from t_user where id = ?";
PreparedStatement preparedStatement = connection.prepareStatement(selectSql);
PreparedStatement preparedStatement_id = connection.prepareStatement(selectSql_id);
preparedStatement_id.setObject(1,1);
ResultSet resultSet = preparedStatement.executeQuery();
ResultSet resultSet_id = preparedStatement_id.executeQuery();
while (resultSet.next()){
System.out.print(resultSet.getString("account")+"----");
System.out.print(resultSet.getString("password")+"----");
System.out.println(resultSet.getString("nickname"));
}
System.out.println();
while (resultSet_id.next()){
System.out.print(resultSet_id.getString("account")+"----");
System.out.print(resultSet_id.getString("password")+"----");
System.out.println(resultSet_id.getString("nickname"));
}
preparedStatement.close();
preparedStatement_id.close();
connection.close();
}
}