【问题标题】:Java SqlServer 2k5 Coonnection ErrorJava Sql Server 2k5 连接错误
【发布时间】:2013-09-08 21:04:16
【问题描述】:

这是我的代码:

public static void addRecord(String name, int index) throws IllegalAccessException, InstantiationException, SQLException, ClassNotFoundException {
  //  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();

    String dbURL = "jdbc:sqlserver://localhost\\sqlexpress";
    String user = "sa";
    String pass = "mypass";
    Connection conn = DriverManager.getConnection(dbURL, user, pass);
    PreparedStatement Prep = conn.prepareStatement("insert into tblAll(AdSoyad,SiraNo) values(?,?);");
    Prep.setString(2, name);
    Prep.setInt(3, index);
    Prep.execute();
    Prep.close();
    Prep = null;
    conn.close();
    conn = null;
}

以下是我得到的错误:

Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The index 3 is out of range.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:171)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setterGetParam(SQLServerPreparedStatement.java:700)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setValue(SQLServerPreparedStatement.java:709)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setInt(SQLServerPreparedStatement.java:870)
    at jmssql.JMsSQL.addRecord(JMsSQL.java:29)
    at jmssql.JMsSQL.main(JMsSQL.java:19)

如果我添加 dbUrl "integratedSecurity=true;"部分;

错误是这样的;

com.microsoft.sqlserver.jdbc.AuthenticationJNI <clinit>
WARNING: Failed to load the sqljdbc_auth.dll cause :- no sqljdbc_auth in java.library.path

我找不到任何解决它的想法,我该如何解决它?

【问题讨论】:

    标签: java sql-server-2005 jdbc


    【解决方案1】:

    错误在这一行

     PreparedStatement Prep = conn.prepareStatement("insert into tblAll(AdSoyad,SiraNo) values(?,?);");
    Prep.setString(2, name);
        Prep.setInt(3, index);
    

    改成

    PreparedStatement Prep = conn.prepareStatement("insert into tblAll(AdSoyad,SiraNo) values(?,?);");
        Prep.setString(1, name);
        Prep.setInt(2, index);
    

    【讨论】:

    • 感谢您的帮助,我已按照您的说明更改了代码并添加了“databaseName=myDB;”到 dbUrl。然后我修复它,谢谢老兄。
    猜你喜欢
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    • 2010-09-06
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多