如何从Eclipse将数据输入MySQL [英] How to input data into MySQL from Eclipse

查看:244
本文介绍了如何从Eclipse将数据输入MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆