在 Eclipse EE 中将数据插入到 sql 表中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28818152/
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
Inserting data into sql table in Eclipse EE
提问by Mr.Smithyyy
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.
我对如何在 Eclipse 中将数据发送到我的数据库感到困惑。我有 Eclipse EE 并且我正确设置了我的数据库开发,但我不明白如何实际发送数据库中的数据。我尝试用 Java 编写内联的 mysql 代码,但这没有用,所以我假设我做错了。
public void sendTime() {
String time = new java.util.Date();
INSERT INTO 'records' ('id', 'time') VALUES (NULL, time);
}
回答by FoggyDay
You actually need to do the following:
您实际上需要执行以下操作:
1) Install MySQL (presumably already done)
1)安装MySQL(想必已经完成了)
2) Download a MySQL JDBC driver and make sure it's in your Eclipse project's CLASSPATH.
2) 下载 MySQL JDBC 驱动程序并确保它在您的 Eclipse 项目的 CLASSPATH 中。
3) Write your program to use JDBC. For example (untested):
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) {
...
}
Here are a couple of good tutorials:
这里有几个很好的教程:
http://www.vogella.com/tutorials/MySQLJava/article.html
http://www.vogella.com/tutorials/MySQLJava/article.html
http://www.mkyong.com/jdbc/jdbc-preparestatement-example-insert-a-record/
http://www.mkyong.com/jdbc/jdbc-preparestatement-example-insert-a-record/
回答by Ankush
This is a demo code for database use in JAVA
这是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)
{
}
Here is a quick guide tutorial which you must read :-
这是您必须阅读的快速指南教程:-