public class dbConnection {
public void testConnection(){
String sql = "select * from emp"; // query
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
InitialContext cxt = new InitialContext();
DataSource dataSource = (DataSource) cxt.lookup("jdbc/myEmpDB"); // "jdbc/myEmpDB"get JNDI JDBC connection
con = dataSource.getConnection();
// run sql objects
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println("Emp Name : "+rs.getString("employeeName")));
}
}
}
Java, Struts 1 & 2, Spring, Hibernate, iBatis, Jasper Reports, RDBMS, JDBC, JSP, Servlet, HTML, JS, AJAX, JQuery
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")));
}
}
}
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")));
}
}
}
Subscribe to:
Posts (Atom)