import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class delt {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//装载驱动
Connection con=null;
PreparedStatement pre=null;
Scanner input=new Scanner(System.in);
System.out.print("输入要删除的姓名:");
String name=input.next();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//得到数据库的连接对象
String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=us";
con=DriverManager.getConnection(url,"sa","123456");
//编写SQL语句
String sql="delete name from st where name=?";
//建立一个预处理对象
pre=con.prepareStatement(sql);
pre.setString(1,name);
System.out.println("11");
int i=pre.executeUpdate();
//con.commit();
System.out.println("22");
if(i>0)
System.out.println("删除成功");
else
System.out.println("操作未成功");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
输入要删除的姓名:王二
11
com.microsoft.sqlserver.jdbc.SQLServerException: 对象名 'name' 无效。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source)
at com.microsoft.sqlserver.jdbc.IOBuffer.processPackets(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.getPrepExecResponse(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PreparedStatementExecutionRequest.executeStatement(Unknown Source)
at com.microsoft.sqlserver.jdbc.CancelableRequest.execute(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeRequest(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(Unknown Source)
at delt.main(delt.java:33)