tmp/src/step4/DeleteRecordTest.java

68 lines
2.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package step4;
import java.util.Scanner;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.io.*;
import org.apache.tools.ant.*;
import org.apache.tools.ant.taskdefs.*;
import org.apache.tools.ant.types.*;
public class DeleteRecordTest {
public static void main(String[] args) throws SQLException {
String tableName ="book";
String authorName = "";
Scanner sc = new Scanner(System.in);
authorName = sc.nextLine();
DeleteRecord deleteTable = new DeleteRecord();
SQLExec sqlExec = new SQLExec();
//设置数据库参数
sqlExec.setDriver(deleteTable.JDBC_DRIVER);
sqlExec.setUrl("jdbc:mysql://localhost/");
sqlExec.setUserid(deleteTable.USER);
sqlExec.setPassword(deleteTable.PASS);
String sqlFile=System.getProperty("user.dir")+File.separator+"challenge4.sql";
try
{
//要执行的脚本
sqlExec.setSrc(new File(sqlFile));
sqlExec.setProject(new Project()); // 要指定这个属性,不然会出错
sqlExec.execute();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Connection connection = deleteTable.getConnection(deleteTable.JDBC_DRIVER, deleteTable.DB_URL, deleteTable.USER, deleteTable.PASS);
int updatedNum = deleteTable.deleteRecordByAuthor(connection,tableName,authorName);
//updatedNum=0说明删除的行数为0删除失败
if(updatedNum == 0)
{
System.out.println(0);
return;
}
ResultSet result = deleteTable.queryDB(connection, tableName);
while (result.next()) {
String id = result.getString("id");
String title= result.getString("title");
String author = result.getString("author");
String publisher = result.getString("publisher");
String publishYear = result.getString("publishYear");
System.out.println(id + " " + title+ " " + author + " "
+ publisher + " " + publishYear);
}
}
}