70 lines
2.4 KiB
Java
70 lines
2.4 KiB
Java
package step6;
|
|
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 UpdateRecordTest {
|
|
public static void main(String args[]) throws SQLException {
|
|
|
|
String tableName = "book";
|
|
|
|
UpdateRecord updateRecord = new UpdateRecord();
|
|
String sqlFile=System.getProperty("user.dir")+File.separator+"challenge6.sql";
|
|
SQLExec sqlExec = new SQLExec();
|
|
//设置数据库参数
|
|
sqlExec.setDriver(updateRecord.JDBC_DRIVER);
|
|
sqlExec.setUrl("jdbc:mysql://localhost/");
|
|
sqlExec.setUserid(updateRecord.USER);
|
|
sqlExec.setPassword(updateRecord.PASS);
|
|
|
|
|
|
try
|
|
{
|
|
//要执行的脚本
|
|
sqlExec.setSrc(new File(sqlFile));
|
|
sqlExec.setProject(new Project()); // 要指定这个属性,不然会出错
|
|
sqlExec.execute();
|
|
}catch (Exception e) {
|
|
// TODO: handle exception
|
|
e.printStackTrace();
|
|
}
|
|
|
|
Connection connection = updateRecord.getConnection(updateRecord.JDBC_DRIVER, updateRecord.DB_URL, updateRecord.USER, updateRecord.PASS);
|
|
Scanner scanner = new Scanner(System.in);
|
|
while(true)
|
|
{
|
|
String input = scanner.nextLine();
|
|
if(input.equals(""))
|
|
break;
|
|
|
|
String[]commands = input.split(" ");
|
|
if(commands.length ==0 || commands.length != 2)
|
|
break;
|
|
|
|
String title = commands[0];
|
|
String publisher = commands[1];
|
|
int updatedNum = updateRecord.updatePublisherByTitle(connection, tableName,title,publisher);
|
|
}
|
|
|
|
|
|
ResultSet result = updateRecord.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);
|
|
|
|
}
|
|
}
|
|
}
|