Reputation: 1329
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);
}
Upvotes: 0
Views: 20107
Reputation: 121649
You actually need to do the following:
1) Install MySQL (presumably already done)
2) Download a MySQL JDBC driver and make sure it's in your Eclipse project's CLASSPATH.
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/
Upvotes: 2
Reputation: 173
This is a demo code for database use in 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 :-
http://www.tutorialspoint.com/jdbc/jdbc-quick-guide.htm
Upvotes: 1