Servlet与数据库的连接

时间:2014-06-23 17:59:45

标签: java servlets

我正在尝试从servlet连接到jdbc数据库。我有单独的类dbconnect用于连接并将数据插入数据库。

这是我的dbconnect类:

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 (\"tsogiaidze@yahoo.com\", \"nika\", \"nika\", \"kaci\")";
        stm.executeUpdate(sql);
      }  
      catch (Exception ex){
          System.out.println(ex.getMessage());
      }
    }
}

在servlet中,我创建了一个新的dbconnect对象,它必须将数据插入到数据库中,但它不会发生。

谁能告诉我为什么它不起作用? 我想我犯了根本错误。

谢谢。

有关更多详细信息,我有jsp文件,它将html表单数据发送到此servlet,然后我尝试使用dbconnect类将这些数据写入数据库。

我已经像这样更改了dbconnect类,现在我使用preparestatement来编写文件,但仍然无效。

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

}

2 个答案:

答案 0 :(得分:2)

使用单引号括起您的文字。

String sql = "INSERT INTO USERS VALUES ("
    + "'tsogiaidze@yahoo.com', 'nika', 'nika', 'kaci')";

或者更好,使用PreparedStatement:

    String sql = "INSERT INTO USERS VALUES (?, ?, ?, ?)");
    PreparedStatement stm = conn.prepareStatement(sql);
    System.out.println("success");
    stm.setString(1, "tsogiaidze@yahoo.com");
    stm.setString(2, "nika");
    stm.setString(3, "nika");
    stm.setString(4, "kaci");
    stm.executeUpdate();

答案 1 :(得分:1)

你的插入语句可能是错误的尝试这个

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

请更改driverclass和connectionString