Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jdbcdemo;
- import java.sql.*;
- public class Driver {
- public static void main(String[] args) throws SQLException {
- Connection myConn = null;
- Statement myStmt = null;
- ResultSet myRs = null;
- try {
- // 1. Get a connection to database
- myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo", "root" , "admin");
- System.out.println("1");
- // 2. Create a statement
- myStmt = myConn.createStatement();
- System.out.println("12");
- // 3. Execute SQL query
- myRs = myStmt.executeQuery("select * from employees");
- System.out.println("123");
- // 4. Process the result set
- while (myRs.next()) {
- System.out.println(myRs.getString("last_name") + ", " + myRs.getString("first_name"));
- }
- }
- catch (Exception exc) {
- exc.printStackTrace();
- System.out.println("1234");
- }
- finally {
- if (myRs != null) {
- myRs.close();
- }
- if (myStmt != null) {
- myStmt.close();
- }
- if (myConn != null) {
- myConn.close();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement