1

I am trying to connect to jdbc database from servlet.I have separate class dbconnect for connection and inserting data to database.

Here is my dbconnect class:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class dbconnect {
    Connection conn;
    Statement stm;

    dbconnect(){
      try{
        Class.forName("org.apache.derby.jdbc.ClientDriver");  
        conn = DriverManager.getConnection
         ("jdbc:derby://localhost:1527/type","nika",
         "mypass");  
        stm = conn.createStatement();
        System.out.println("success");
        String sql = "INSERT INTO USERS " + "VALUES (\"[email protected]\", \"nika\", \"nika\", \"kaci\")";
        stm.executeUpdate(sql);
      }  
      catch (Exception ex){
          System.out.println(ex.getMessage());
      }
    }
}

In servlet I create new dbconnect object which must insert data to database,but it does not happen.

Can anyone tell me why it does not work? I guess I make fundamental mistake.

Thank you.

For more details I have jsp file which sends html form data to this servlet and then I try to write these data to database using dbconnect class.

I have changed dbconnect class like this,now I use preparedstatement to write files but still does not work.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;

public class dbconnect {
    Connection conn;
    Statement stm;

    dbconnect(){
      try{
        Class.forName("org.apache.derby.jdbc.ClientDriver");  
        conn = DriverManager.getConnection("jdbc:derby://localhost:1527/type","nika","nika");  
        stm = conn.createStatement();
        System.out.println("success");
        String sql = "INSERT INTO NIKA.USERS (mail, pass, gender, saxeli) VALUES (?, ?, ?, ?)";
        PreparedStatement statement = conn.prepareStatement(sql);
        statement.setString(1, "tsogiaidze");
        statement.setString(2, "nika");
        statement.setString(2, "nika");
        statement.setString(2, "kaci");
        int rowsInserted = statement.executeUpdate();
        if (rowsInserted > 0) {
        System.out.println("A new user was inserted successfully!");
        }
      }  
      catch (Exception ex){
          System.out.println(ex.getMessage());
      }
    }

}

5
  • 1
    As noted, check any exceptions, otherwise first check that you can manually execute the INSERT statment and if that works then attach a debugging session and step through. Commented Jun 23, 2014 at 18:06
  • If you mind about security that I am not closing connections and so on I do not care about it yet.Can you tell me exactly what is bad practice in my code?Servlet works itself,but does not add any values to the database. Commented Jun 23, 2014 at 18:06
  • Which error do you get ? Commented Jun 23, 2014 at 18:26
  • I do not get any error or exception. Commented Jun 23, 2014 at 18:53
  • 1
    That is strange you don't get an exception. Maybe confirm this by writing more to stdout (ex.getMessage can be empty, relying on the message alone is not a good idea), also see what the return value of executeUpdate is. Commented Jun 23, 2014 at 19:01

3 Answers 3

2

Use single quote to surround your texts.

String sql = "INSERT INTO USERS VALUES ("
    + "'[email protected]', 'nika', 'nika', 'kaci')";

Or better, use a PreparedStatement:

    String sql = "INSERT INTO USERS VALUES (?, ?, ?, ?)");
    PreparedStatement stm = conn.prepareStatement(sql);
    System.out.println("success");
    stm.setString(1, "[email protected]");
    stm.setString(2, "nika");
    stm.setString(3, "nika");
    stm.setString(4, "kaci");
    stm.executeUpdate();
Sign up to request clarification or add additional context in comments.

Comments

1

Your insert statement is possibly wrong try this

       try{
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("making connection");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "welcome");
            PreparedStatement stmt=conn.prepareStatement("INSERT INTO student(student_name,student_id,class) VALUES(?,?,?)");
            stmt.setString(1, "abc");
            stmt.setString(2, "4");
            stmt.setString(3, "ukg");
            stmt.executeUpdate();
            System.out.println("query executed");
        }
        catch (ClassNotFoundException e) {
        e.printStackTrace();
      } catch (SQLException e) {
        e.printStackTrace();
        }

Please change the driverclass and connectionString

3 Comments

Why mysql?I try to connect to jdbc.
M.Sharma throws exception from this line Class.forName("org.apache.derby.jdbc.ClientDriver");
Hi told you to change the driverclass and connection-string and please have the required jars in classpath. jar you can get here
0

in my case I used PostgreSQL with maven add in pom.xml the following code

<dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.3.2</version>
</dependency>

in servlet class

Class.forName("org.postgresql.Driver").newInstance();
        Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/" + DB_NAME, USER, PASS);

Comments

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.