将数据从Java类插入数据库

时间:2013-12-07 20:11:20

标签: java postgresql jdbc insert

所以我试图将保存在字段中的数据插入到我的数据库中,这当前不起作用。如果我手动输入数据,例如INSERT INTO customer('value1','value2','value3'),我可以通过Java类将信息输入我的数据库;等,但是当我使用PreparedStatements等时,它不会输入任何数据。

    String customer_name = request.getParameter("name"); //grab values from bookingForm
    String customer_email = request.getParameter("email");
    String billing_address = request.getParameter("bAddress");
    String card_type = request.getParameter("card_type");
    String card_exp = request.getParameter("card_exp");
    String cardno = request.getParameter("Cardnum");
    String insertSQL;
     try {{
            try (Connection conn = database.getConnection()) {
                insertSQL = "INSERT INTO customer(customer_name, customer_email, billing_address, card_type, card_exp, cardno) VALUES(?, ?, ?, ?, ?, ?)";
                try (PreparedStatement ps = conn.prepareStatement(insertSQL)) {
                    ps.setString(1, customer_name);
                    ps.setString(2, customer_email);
                    ps.setString(3, billing_address);
                    ps.setString(4, card_type);
                    ps.setString(5, card_exp);
                    ps.setString(6, cardno);
                    ps.executeUpdate();

                    conn.close();
                }
            }

    }


} catch (    ClassNotFoundException | SQLException ex) {
    Logger.getLogger(PayForm.class.getName()).log(Level.SEVERE, null, ex);
}

2 个答案:

答案 0 :(得分:1)

你在哪里加载postgres驱动程序?

Connection c = null;
  try {
     Class.forName("org.postgresql.Driver");
     c = DriverManager
        .getConnection("jdbc:postgresql://localhost:5432/testdb",
        "postgres", "123");
  } catch (Exception e) {
     e.printStackTrace();
     System.err.println(e.getClass().getName()+": "+e.getMessage());
     System.exit(0);
  }
  System.out.println("Opened database successfully");
}

并且您的代码中似乎还有一个额外的{大括号。

答案 1 :(得分:1)

befor conn.close();写conn.commit(); 我有你的问题,这对我有用! 我希望能帮助你:) 我的代码如下:

package servlets;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.text.*;


import java.util.Date;
/**
 * Created by ALI7 on 8/7/2015.
 */
@WebServlet(name = "addnote")

public class addnote extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request,response);
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    Connection c ;
    PreparedStatement pst ;
    response.setContentType("text/html");
    PrintWriter out= response.getWriter();
    try
    {
        Class.forName("org.postgresql.Driver");
        c = DriverManager
                .getConnection("jdbc:postgresql://localhost:5432/postgres",
                        "postgres", "1");
        c.setAutoCommit(false);


        String stm = "insert into note (note_id,name,text,date,time) values (default,?,?,?,?)";
        pst = c.prepareStatement(stm);

        String dateString = request.getParameter("date");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date convertedDate = dateFormat.parse(dateString);
        String timeString = request.getParameter("time");
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
        Date convertedtime = timeFormat.parse(timeString);

        pst.setString(1,request.getParameter("name"));
        pst.setString(2, request.getParameter("text") );
        pst.setDate(3, new java.sql.Date(convertedDate.getTime()));
        pst.setTime(4,new java.sql.Time(convertedtime.getTime()));

        int i = pst.executeUpdate();

        if(i>0)
            out.println("Inserted Successfully");
        else
            out.println("Insert Unsuccessful");

        pst.close();
        c.commit();
        c.close();
    } catch ( Exception e ) {
        System.err.println( e.getClass().getName()+": "+ e.getMessage() );
        System.exit(0);
    }

}
}