繁体   English   中英

在Eclipse EE中将数据插入sql表

[英]Inserting data into sql table in Eclipse EE

我对如何在Eclipse中将数据发送到数据库感到困惑。 我具有Eclipse EE,并且可以正确设置数据库开发,但是我不了解如何实际发送数据库中的数据。 我尝试用Java内联编写mysql代码,但这没有用,所以我假设我做错了。

public void sendTime() {
    String time = new java.util.Date();
    INSERT INTO 'records' ('id', 'time') VALUES (NULL, time);
}

您实际上需要执行以下操作:

1)安装MySQL(大概已经完成)

2)下载一个MySQL JDBC驱动程序,并确保它在您的Eclipse项目的CLASSPATH中。

3)编写程序以使用JDBC。 例如(未测试):

 private Connection connect = null;
 private Statement statement = null;
 try {
    Class.forName("com.mysql.jdbc.Driver");
    connect =
      DriverManager.getConnection("jdbc:mysql://localhost/xyz?"
          + "user=sqluser&password=sqluserpw");
      String query = "INSERT INTO records (id, time) VALUES (?, ?)";
      PreparedStatement preparedStmt = conn.prepareStatement(query);
      preparedStmt.setInt (1, null);
      preparedStmt.setDate (2, new java.util.Date());
      preparedStmt.executeUpdate();
    } catch (SQLException e) {
      ...
    }

这里有一些很好的教程:

http://www.vogella.com/tutorials/MySQLJava/article.html

http://www.mkyong.com/jdbc/jdbc-preparestatement-example-insert-a-record/

这是JAVA中数据库使用的演示代码

    try
    {
        Class.forName("com.mysql.jdbc.Driver");

        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_name","uname","password");

        Statement st = con.createStatement();

        String stmt = "Type your sql INSERT statement here";
        st.execute(stmt);

    }
    catch (Exception ex)
    {

    } 

这是一个快速指南教程,您必须阅读:-

http://www.tutorialspoint.com/jdbc/jdbc-quick-guide.htm

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM