Advertisement
Guest User

SQL conn

a guest
Apr 22nd, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.10 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package jdbcsqlex1;
  7. import java.sql.*;
  8.  
  9. /**
  10.  *
  11.  * @author franciscocarvalho
  12.  */
  13. public class JdbcSQLex1 {
  14.  
  15.     /**
  16.      * @param args the command line arguments
  17.      */
  18.     public static void main(String[] args) throws SQLException, ClassNotFoundException {
  19.         // TODO code application logic here
  20.         Class.forName("com.mysql.jdbc.Driver");
  21.        
  22.         //---- define connection
  23.         Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/java","root","");
  24.        
  25.        //----- define statement
  26.        Statement stat = connect.createStatement();
  27.        
  28.        //----- define query
  29.        ResultSet rset = stat.executeQuery("select * from books");
  30.        
  31.        //----- visualize data
  32.        while (rset.next()){
  33.            System.out.println(rset.getString(1) + "\t" + rset.getString(2) + "\n" + rset.getString(3));
  34.        }
  35.        
  36.        
  37.     }
  38.    
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement