程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> mssql-delete出現的兩個錯誤

mssql-delete出現的兩個錯誤

編輯:編程解疑
delete出現的兩個錯誤

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]+"',";//中間的學號
        }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved