I'm trying to input data into MySQL database through eclipse. Here's what I have so far:
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import com.mysql.jdbc.Statement;
public class MySQL{
public static void main(String[] args)throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/database","root","root");
PreparedStatement stmt = con.prepareStatement("INSERT INTO 'database'.'Table'(Account_ID,First_Name,Last_Name) VALUES ('hello12','Ed','Lee')");
ResultSet result = stmt.executeQuery();}
}
It doesn't work for some reason...any ideas?
Change your code likes this;
Change the following line
to
the single quotes used for the database name and the table name will throw a syntax error. Instead use ` this symbol.
Have you tried committing?
con.commit();
As suggested by others you need to commit the changes you made in your sql statements. Here is how it should look:
Import things to note that have been changed.
PreparedStatement
replaces the new values with?
syntax instead of being hard coded. In a real application those will end up being passed in, and if you just did String concatenation, you would be opening yourself up to SQL injection or other problems.