PreparedStatement( DML语句)insert,delete,update
package cn.usts.edu.jdbc;
import com.mysql.jdbc.Driver;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;
public class PreparedStatementDemo {
@Test
public void crud() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, IOException {
Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
Driver driver =(Driver) aClass.newInstance();
DriverManager.registerDriver(driver);
FileInputStream fileInputStream = new FileInputStream("D:\\all_projects\\java_projects\\java_ij\\springMVC\\Jdbc\\src\\cn\\usts\\edu\\config\\db.properties");
Properties properties = new Properties();
properties.load(fileInputStream);
String url=(String) properties.get("url");
String user=(String) properties.get("user");
String password=(String) properties.get("password");
Connection connection = DriverManager.getConnection(url, user, password);
Scanner scanner = new Scanner(System.in);
System.out.println("输入用户名");
String name = scanner.nextLine();
System.out.println("输入密码");
String psd = scanner.nextLine();
String sql ="delete from admin where amin=?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,name);
int rows = preparedStatement.executeUpdate();
System.out.println("执行行数"+rows);
preparedStatement.close();
connection.close();
}
}