Pages

Friday, April 24, 2009

JDBC (Java Database Connectivity) with MySQL

public class dbConnection {

public void testConnection(){

String sql = "select * from emp"; // query
String conURL = "jdbc:mysql://localhost:3306/employeeDB"; // "employeeDB" is database name

Connection con = null;
Statement stmt = null;
ResultSet rs = null;

Class.forName("com.mysql.jdbc.Driver"); // create instance of mysql driver
con = DriverManager.getConnection(conURL);

stmt = con.createStatement();
rs = stmt.executeQuery(sql);

while (rs.next()) {
System.out.println("Emp Name : "+rs.getString("employeeName")));
}
}
}

No comments:

Google
 

Java-Struts