장나라 장나라 - 2 months ago 11
Java Question

can't insert data to mysql

I can connect fine in Netbeans using this:

Connection conn = null;
try{
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/librarymangementsystem","root","root");
if(conn!= null)
{
System.out.println("connected");
}
}catch(Exception e)
{
System.out.println("not connected");
}
}


But when it comes to adding data to the columns, i just cant.

try{
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/librarymangementsystem","root","root");
Statement stmt=conn.createStatement();
String Query = "INSERT into librarian_details(username, name, email, password) VALUES("+ uname +", "+ fname +", " + emails + ", " + psword +")";
stmt.executeUpdate(Query);
JOptionPane.showMessageDialog(null, "Success!");
}


Anyone knows the problem?

Answer

I guess the username, name, email and password fields are of String type and Data type used while creating the columns username, name, email and password of librarian_details is Varchar .

If so, then you need to update your query string to the below code:

String Query = "INSERT into librarian_details(username, name, email, password) 
VALUES('"+ uname +"','"+ fname +"','" + emails + "','" + psword +"')";