You are on page 1of 1

JDBC Connectivity

import java.sql.*;
public class prac15 {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn =
DriverManager.getConnection("jdbc:mysql://localhost/EMP","root","root");
stmt = conn.createStatement();
String sql = "SELECT id, first, last FROM employees";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
int id = rs.getInt("id");
String first = rs.getString("first");
String last = rs.getString("last");
System.out.print("ID: " + id);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}

You might also like