0

i have a connection method like this:

public Connection getConnection(){
   Connection con = null;
   try {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:andre",User,Pass);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }   
   } catch (SQLException e) {
       e.printStackTrace();
   }
   return con;
}

and i want to user insert query with dynamic function, and i use code like this:

public void setInsert(String username, String password) throws SQLException{
   Connection con = getConnection();
   Statement stmt = con.createStatement();
   String query = "INSERT INTO andre(username,password) VALUES("+username+","+password+")";
   stmt.executeQuery(query);
   con.close();
   stmt.close();

}

and i test my code using jUnit.

@Test
 public void insertTest() throws SQLException{
 test.SetInsert(null, null);
 }

why i get an error?, how i fix it. please help me. sorry if my grammar is so bad. Thanks for your answers.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332

2 Answers2

0

Avoid using Statement and use PreparedStatement instead. This way you can set parameters for your query instead of concatenating Strings. Also, you should close the PreparedStatement before closing the connection.

String query = "INSERT INTO andre(username,password) VALUES(?, ?)";
//PreparedStatement supports parameters, Statement doesn't
PreparedStatement stmt = con.prepareStatement(query);
stmt.setString(1, username);
stmt.setString(2, password);
stmt.executeUpdate();
//check the order of execution
stmt.close();
con.close();

More info on the advantages of PreparedStatement over Statement:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • but i still get an error like: "no suitable method found for createStatement(String) method Connection.createStatement() is not applicable (actual and formal argument lists differ in length) method Connection.createStatement(int,int) is not applicable (actual and formal argument lists differ in length) method Connection.createStatement(int,int,int) is not applicable (actual and formal argument lists differ in length)" – user3497360 Jun 04 '14 at 14:37
  • but in my test, i still get an error like "cannot find symbol symbol: method SetInsert(,) location: variable test of type ConnectionSql" – user3497360 Jun 04 '14 at 14:46
  • @user3497360 Java is case sensitive. You should use the exact name of the method: `SetInsert` **is not the same than** `setInsert`. – Luiggi Mendoza Jun 04 '14 at 14:47
  • @user3497360 this is not a forum that works with long threads by emails or something similar. Please [edit](http://stackoverflow.com/posts/24040101/edit) your question and post the relevant data that help us to identify the error, like your current piece off code and the associated stacktrace. – Luiggi Mendoza Jun 04 '14 at 14:58
0

There is a typo in you code. You have to call

test.setInsert(...)

(with a lowercase s).

Jan
  • 2,060
  • 2
  • 29
  • 34