0

This is my Code:

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;
}

The following is the error i am getting:

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)

if i add dbUrl "integratedSecurity=true;" part;

error is that;

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

i could not find any idea fix it, how can i fix it?

1 Answer 1

1

Error is in this line

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

change to

PreparedStatement Prep = conn.prepareStatement("insert into tblAll(AdSoyad,SiraNo) values(?,?);");
    Prep.setString(1, name);
    Prep.setInt(2, index);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for helping, i've changed my code as u tell and add "databaseName=myDB;" to dbUrl. Then i fix it, thanx dude.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.