Advertisement
Guest User

Untitled

a guest
Jan 20th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.65 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 jdbc_example;
  7.  
  8. import java.sql.*;
  9. import java.util.Scanner;
  10.  
  11. /**
  12.  *
  13.  * @author raynn
  14.  */
  15. public class Jdbc_example {
  16.  
  17.     /**
  18.      * @param args the command line arguments
  19.      */
  20.     static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  21.  
  22.    
  23.     public static void main(String[] args) {
  24.         String DB_URL;  
  25.         String USER;
  26.         String PASS;
  27.         System.out.println("welcome to jdbc example");
  28.        
  29.         Scanner s = new Scanner(System.in);
  30.        
  31.         System.out.println("please enter your databases url!");
  32.         DB_URL = s.nextLine();
  33.        
  34.         System.out.println("please enter username");
  35.         USER = s.nextLine();
  36.        
  37.         System.out.println("please enter password");
  38.         PASS = s.nextLine();
  39.        
  40.         Connection conn = null;
  41.         Statement stmt = null;
  42.        
  43.         try{
  44.             //Class.forName("com.mysql.jdbc.Driver");
  45.             Class.forName("com.mysql.jdbc.Driver");
  46.            
  47.             System.out.println("connecting to database");
  48.             conn = DriverManager.getConnection(DB_URL, USER, PASS);
  49.            
  50.             System.out.println("creating statement");
  51.             stmt = conn.createStatement();
  52.             String query;
  53.             query = "put query here";               //enter query
  54.             ResultSet rss = stmt.executeQuery(query);
  55.            
  56.             while(rss.next()){
  57.                 int id = rss.getInt("id");          // put tables into vars
  58.                 String str = rss.getString("str");
  59.                
  60.                 System.out.print("ID: " + id);      //output
  61.                 System.out.println("str: " + str);
  62.             }
  63.            
  64.             rss.close();                            //cleanup
  65.             stmt.close();
  66.             conn.close();
  67.            
  68.         }catch(SQLException se){
  69.             se.printStackTrace();
  70.         }catch(Exception e){
  71.             e.printStackTrace();    //handle errors for Class.forName
  72.         }finally{
  73.             try{
  74.                 if(stmt != null)
  75.                     stmt.close();
  76.             }catch(SQLException se2){
  77.             }
  78.            
  79.             try{
  80.                 if(conn != null)
  81.                     conn.close();
  82.             }catch(SQLException se){
  83.                 se.printStackTrace();
  84.             }
  85.         }
  86.            
  87.         System.out.println("finished ending!");
  88.            
  89.        
  90.        
  91.     }
  92.    
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement