Thursday, October 14, 2010

Java Connecting to MySQL Database

Download MySQL Connector/J
You will need to download MySQL Connector/J from mySQL website under the deverloper's site. (http://dev.mysql.com last checked in October 2010).

Put it into a location that you can remember

Source Code
Need to import
import java.sql.*;

Code
Connection conn;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost/dbschema", "username", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from table");

while (rs.next()) {
  ... [rs.getString(""), etc]
}
rs.close();
conn.close();

No comments:

Post a Comment