如何从Eclipse将数据输入MySQL [英] How to input data into MySQL from Eclipse
本文介绍了如何从Eclipse将数据输入MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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);
连接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();}
}
由于某种原因,它不起作用...任何想法?
解决方案
更改以下行
PreparedStatement stmt = con.prepareStatement(INSERT INTO'database'。'Table'(Account_ID,First_Name,Last_Name)VALUES hello12' , '埃德', '李'));
至
PreparedStatement stmt = con.prepareStatement(INSERT INTO`database`.`Table`(Account_ID,First_Name,Last_Name)VALUES('hello12','Ed','Lee'));
用于数据库名称和表名的单引号将抛出语法错误。而是使用这个符号。
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 the following line
PreparedStatement stmt = con.prepareStatement("INSERT INTO 'database'.'Table'(Account_ID,First_Name,Last_Name) VALUES ('hello12','Ed','Lee')");
to
PreparedStatement stmt = con.prepareStatement("INSERT INTO `database`.`Table`(Account_ID,First_Name,Last_Name) VALUES ('hello12','Ed','Lee')");
the single quotes used for the database name and the table name will throw a syntax error. Instead use ` this symbol.
这篇关于如何从Eclipse将数据输入MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文