2

So i am trying to insert data saved in fields into my database, this currently doesn't work. I can put information into my database via the Java class if i am manually inputting data eg- INSERT INTO customer('value1','value2','value3'); etc but it doesn't input any data when i use PreparedStatements etc.

    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);
}
7
  • @AmitSharma sorry am relatively new to this, the catch at the bottom isn't enough? Commented Dec 7, 2013 at 20:19
  • that's sufficient, but do you see any exceptions in log? Did you check the log? Commented Dec 7, 2013 at 20:21
  • @Amit Sharma code is quite enough to solve the issue. Commented Dec 7, 2013 at 20:27
  • @AmitSharma checkd, no exceptions Commented Dec 7, 2013 at 20:29
  • 1
    @Amit shrma see question tags.:) Commented Dec 7, 2013 at 20:32

2 Answers 2

1

where did you load the postgress driver?

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

and also it seems ther is an extra { braces in your code.

Sign up to request clarification or add additional context in comments.

4 Comments

my postgres driver, log in etc is in a different class that i call upon here - Connection conn = database.getConnection() (Im pretty sure that calls it?)
why did you put try{{
remove that { after try{ and remove } before }catch
Got rid of the braces + uploaded my database class + the code above to here: pastebin.com/fmTWB3Lx . Sorry not so good at formatting on this site, thank you for bearing with me.
1

befor conn.close(); write conn.commit(); i have your problem and this work for me! i hope help you :) my code that work is follow:

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

}
}

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.