如何从 Eclipse 向 MySQL 输入数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11893462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 18:47:21  来源:igfitidea点击:

How to input data into MySQL from Eclipse

javamysqleclipseinsert

提问by Ed Lee

I'm trying to input data into MySQL database through eclipse. Here's what I have so far:

我正在尝试通过 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");

    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?

由于某种原因它不起作用......有什么想法吗?

回答by km1

Have you tried committing?

你试过承诺吗?

con.commit();

con.commit();

回答by Sai Ye Yan Naing Aye

Change your code likes this;

像这样改变你的代码;

    public class MySQL{
          public static void main(String[] args)throws Exception{
              Class.forName("com.mysql.jdbc.Driver");
              try{
                  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 (?,?,?)");
                   stmt.setString(1, "hello12");
                   stmt.setString(2, "Ed");
                   stmt.setString(3, "Lee");
                   stmt.executeUpdate();
              } catch (Exception e) {
                   e.printStackTrace();
              } finally {
                   stmt.close();
                   con.close();
              }
          }

     }

回答by Jacob Schoen

As suggested by others you need to commit the changes you made in your sql statements. Here is how it should look:

正如其他人所建议的那样,您需要提交您在 sql 语句中所做的更改。这是它的外观:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class MySQL{

    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = null;
        PreparedStatement stmt = null;
        try {
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/database", "root", "root");
            stmt = con.prepareStatement("INSERT INTO 'database'.'Table'(Account_ID,First_Name,Last_Name) VALUES (?,?,?)");
            stmt.setString(1, "hello12");
            stmt.setString(2, "Ed");
            stmt.setString(3, "Lee");
            stmt.executeUpdate();
            conn.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stmt != null) {
                try {
                   stmt.close();
                } catch (SQLException ex) {
                }
            }
            if (con != null) {
                try {
                   con.close();                    
                } catch (SQLException ex) {
                }
            }
        }
    }
}

Import things to note that have been changed.

导入要注意的东西已经改变了。

  • You declare the variables before the try block so that they are accessible in the finally block, but initialize them inside the try block in case they throw an exception.
  • The PreparedStatementreplaces 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.
  • The finally block is used to free any resources regardless if an exception is thrown. So say you create the connection and prepared statement, but an error gets thrown executing it. The code in the finally block will still run to close the statement and connection, freeing those resources.
  • 在 try 块之前声明变量,以便在 finally 块中可以访问它们,但在 try 块中初始化它们,以防它们抛出异常。
  • PreparedStatement与更换新的价值观?的语法,而不是被硬编码。在实际应用程序中,这些最终会被传入,如果您只是进行字符串连接,您将面临 SQL 注入或其他问题。
  • finally 块用于释放任何资源,无论是否抛出异常。因此,假设您创建了连接和准备好的语句,但是执行它时会引发错误。finally 块中的代码仍将运行以关闭语句和连接,从而释放这些资源。

回答by Akshatha Srinivas

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.

用于数据库名称和表名称的单引号将引发语法错误。而是使用 ` 这个符号。