Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.80 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class HelloWorld {
  4.     public static void main(String[] args) {
  5.         Connection con = null;
  6.  
  7.         try {
  8.           Class.forName("com.mysql.jdbc.Driver").newInstance();
  9.           con = DriverManager.getConnection("jdbc:mysql:///carlsdb", "root", "14142135");
  10.  
  11.           if(!con.isClosed()) {
  12.             System.out.println("Successfully connected to MySQL server...");
  13.             Statement stmt = con.createStatement();
  14.             ResultSet rs = stmt.executeQuery("SELECT * FROM testtable;");
  15.             rs.next();
  16.             System.out.println(rs.getString("name"));
  17.           }
  18.         } catch(Exception e) {
  19.           System.err.println("Exception: " + e.getMessage());
  20.         } finally {
  21.           try {
  22.             if(con != null)
  23.               con.close();
  24.           } catch(SQLException e) {}
  25.         }  
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement