create table teacher(tno char(6) primary key,
tname varchar(20) not null,
authority varchar(10) not null,
password varchar(20) not null
);
insert into teacher values('xg2221','鐘飛','teacher','123456');
insert into teacher values('111111','張歡歡','teacher','123456');
有兩個錯誤:
1、、、、、、、、、、、、、、、、、、、、、、
public class TeacherDaoImpl{
public static void main(String[] args) {
TeacherDaoImpl tdi=new TeacherDaoImpl();
String[] str={"111111"};
tdi.deleteTeacherByNo(str);
public int deleteTeacherByNo(String[] tno) {
con=DBUtils_mssql.getCon();
String tnos="";
for(int i=0;i<tno.length;i++){
if(i==tno.length-1){
tnos=tnos+tno[i]+")";//最後一個學號
}else{
tnos=tnos+tno[i]+",";//中間的學號
}
}
String sql="delete from Teacher where tno in ("+tnos;
int i=0;
try {
/*
* delete from teacher where tno in (tno1,tno2,...)
* */
ps=con.prepareStatement(sql);
i=ps.executeUpdate();
return i;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
}
}
1、運行代碼會出現錯誤:
com.microsoft.sqlserver.jdbc.SQLServerException: Conversion failed when converting the varchar value 'xg2221' to data type int.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:217)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1635)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:426)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:372)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:6276)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1793)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:184)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:159)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:315)
at com.student.daoImpl.TeacherDaoImpl.deleteTeacherByNo(TeacherDaoImpl.java:89)
at com.student.daoImpl.TeacherDaoImpl.main(TeacherDaoImpl.java:28)
2、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
public static void main(String[] args) {
TeacherDaoImpl tdi=new TeacherDaoImpl();
String[] str={"111111"};
tdi.deleteTeacherByNo(str);
}
要是將main函數改成下面的代碼
public class TeacherDaoImpl{
public static void main(String[] args) {
TeacherDaoImpl tdi=new TeacherDaoImpl();
String[] str={"xg2221"};
tdi.deleteTeacherByNo(str);
}
public int deleteTeacherByNo(String[] tno) {
con=DBUtils_mssql.getCon();
String tnos="";
for(int i=0;i<tno.length;i++){
if(i==tno.length-1){
tnos=tnos+tno[i]+")";//最後一個學號
}else{
tnos=tnos+tno[i]+",";//中間的學號
}
}
String sql="delete from Teacher where tno in ("+tnos;
int i=0;
try {
/*
* delete from teacher where tno in (tno1,tno2,...)
* */
ps=con.prepareStatement(sql);
i=ps.executeUpdate();
return i;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
}
2、就會出現如下的提示:
com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name 'xg2221'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:217)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1635)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:426)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:372)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:6276)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1793)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:184)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:159)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:315)
at com.student.daoImpl.TeacherDaoImpl.deleteTeacherByNo(TeacherDaoImpl.java:89)
at com.student.daoImpl.TeacherDaoImpl.main(TeacherDaoImpl.java:28)
if(i==tno.length-1){
tnos=tnos+tno[i]+")";//最後一個學號
}else{
tnos=tnos+tno[i]+",";//中間的學號
}
改成
if(i==tno.length-1){
tnos=tnos+"'"+tno[i]+"')";//最後一個學號
}else{
tnos=tnos+"'" + tno[i]+"',";//中間的學號
}