Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Shammikit

Having trouble inserting data to mysql db through java

Recommended Posts

Posted · Original PosterOP

i want to insert some data to a table in my mysql db.firstly i got a null pointer exception error and after changing  rs= st.executeQuery(query); to rs= st.executeUpdate(query); i get no errors but no values are being sent to the db.i have correctly setup the db drivers and connection and i have checked my insert statement.all these look correct.please help me 

Link to post
Share on other sites
Posted · Original PosterOP
2 minutes ago, Lacrimas said:

Giving us some code would be useful.

try{ 
String query = "INSERT INTO table1(Emp_no,Date,salary) VALUES ('"+empid+"','"+localDate+"','"+w_amount+"')";
       ps.executeUpdate(query);
}

catch(Exception e)
{
System.out.print(e);
}  

the variables have been declared above the try statement

 

Link to post
Share on other sites
Posted · Original PosterOP
9 minutes ago, Lacrimas said:

String query = "INSERT INTO table1(Emp_no,Date,salary) VALUES ('"+empid+"','"+localDate+"','"+w_amount+"')";

should be


String query = "INSERT INTO table1 (Emp_no, Date, salary)" + "VALUES ('"+empid+"','"+localDate+"','"+w_amount+"')";

 

i get this now

 

Error: java.lang.NumberFormatException: For input string: ""

Link to post
Share on other sites
String query = "INSERT INTO table1(Emp_no,Date,salary) VALUES (?, ?, ?)";

PreparedStatement pStatement;
try {
  // getConnection() is DriverManager.getConnection(); I have things set up in seperate classes
  pStatement = getConnection().prepareStatement(query);

  pStatement.setInt(1, empid);
  // you should be able to do setDate() if thats how your DB is set up
  pStatement.setString(2, localDate);
  pStatement.setInt(3, w_amount);

  pStatement.executeUpdate();
} catch (SQLException ex) {
  Logger.getLogger(DBInterface.class.getName()).log(Level.SEVERE, null, ex);
}

I've done this before. This is what I did.

 

 

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Newegg

×