0

Below, there's code that should insert information to the MySQL database. It works without exceptions, but does not insert information to the database.

    try {
        Class.forName("Com.mysql.jdbc.Driver");
        Connection connection= DriverManager.getConnection("jdbc:mysql:///mymail","root","toor");
        out.print("Connected");
        PreparedStatement ps= connection.prepareStatement("insert into mytable(fname,lname,uname,pass,cpass,month,day,year,Gender,mobile,email,country) values (?,?,?,?,?,?,?,?,?,?,?,?)");
    ps.setString(1, fname);
    ps.setString(2, lname);
    ps.setString(3, uname);
    ps.setString(4, pass);
    ps.setString(5, cpass);
    ps.setString(6, month);
    ps.setString(7, day);
    ps.setString(8, year);
    ps.setString(9, Gender);
    ps.setString(10, mobile);
    ps.setString(11,email);
    ps.setString(12, country);
    ps.executeUpdate();

    } catch (Exception e) 
                        {}
4
  • You don't display your whole code, but I suspect that getting no error is the result of silently swallowing it in that catch statement. Commented Jul 12, 2014 at 11:22
  • 1
    Can we see code handling Exception? Commented Jul 12, 2014 at 11:22
  • if you are not viewing any exception.. add this in your catch block e.printStackTrace();for viewing full trace or System.out.println(e); for viewing the exception alone.. P.S: Always remember to post your exceptions or errors when you ask a question. That will help us in answering your question better. We cant assume all possible errors.. So, to get the right answer post your errors/exception Commented Jul 12, 2014 at 13:02
  • There was neither any error nor any exception.it compiles succesfully but then also there is not any insertion in database. and i have already added e.printStackTrace(); but result is same. Commented Jul 12, 2014 at 17:51

2 Answers 2

1

It might be that your connection is not in autocommit mode, so you have to call a connection.commit().

And you must call connection.close() (in a finally block) before you leave that method, otherwise you will leak open connections

I would suggest you try a framework that saves you a lot of those troubles like spring with JdbcTemplates.

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

2 Comments

In java 7, is it necessary to close jdbc connection ? I think not.
But then you would have to use a try block with try-with-ressource
0

Well without the complete code and table structure it's hard to tell what is causing the problem. Though I would suggest you check the type of your columns with the values you are inserting. That's potentially could be causing the problem.

Secondly the url you are specifying for database looks suspicious.

And lastly make sure you have a mysql connector in your classpath.

If you've checked the above points, I think you should be good.

1 Comment

Ohh thanks, actually i didn't had used mysql connector. Now i got it.

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.