将数据插入到Eclipse EE中的sql表中 [英] Inserting data into sql table in Eclipse EE
问题描述
我很困惑如何在Eclipse中将数据发送到我的数据库。我有Eclipse EE,我正确设置我的数据库开发,但我不明白如何实际发送数据库中的数据。我尝试用Java编写的mysql代码,但是没有这样做,所以我假设我做错了。
I'm confused on how to send data to my database in Eclipse. I have Eclipse EE and I set up my database development correctly but I don't understand how to actually send the data in the database. I tried writing the mysql code inline with the Java but that didn't work so I'm assuming I'm doing it way wrong.
public void sendTime() {
String time = new java.util.Date();
INSERT INTO 'records' ('id', 'time') VALUES (NULL, time);
}
推荐答案
你实际上需要做以下:
1)安装MySQL(大概已经完成)
1) Install MySQL (presumably already done)
2)下载一个MySQL JDBC驱动程序,确定它在Eclipse项目的CLASSPATH中。
2) Download a MySQL JDBC driver and make sure it's in your Eclipse project's CLASSPATH.
3)编写程序以使用JDBC。例如(未测试):
3) Write your program to use JDBC. For example (untested):
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) {
...
}
以下是几个很好的教程:
Here are a couple of good tutorials:
http://www.vogella.com/tutorials/MySQLJava/article.html
http://www.mkyong.com/jdbc/jdbc-preparestatement-example-insert-a-record/
这篇关于将数据插入到Eclipse EE中的sql表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!