Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package jdbc_example;
- import java.sql.*;
- import java.util.Scanner;
- /**
- *
- * @author raynn
- */
- public class Jdbc_example {
- /**
- * @param args the command line arguments
- */
- static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
- public static void main(String[] args) {
- String DB_URL;
- String USER;
- String PASS;
- System.out.println("welcome to jdbc example");
- Scanner s = new Scanner(System.in);
- System.out.println("please enter your databases url!");
- DB_URL = s.nextLine();
- System.out.println("please enter username");
- USER = s.nextLine();
- System.out.println("please enter password");
- PASS = s.nextLine();
- Connection conn = null;
- Statement stmt = null;
- try{
- //Class.forName("com.mysql.jdbc.Driver");
- Class.forName("com.mysql.jdbc.Driver");
- System.out.println("connecting to database");
- conn = DriverManager.getConnection(DB_URL, USER, PASS);
- System.out.println("creating statement");
- stmt = conn.createStatement();
- String query;
- query = "put query here"; //enter query
- ResultSet rss = stmt.executeQuery(query);
- while(rss.next()){
- int id = rss.getInt("id"); // put tables into vars
- String str = rss.getString("str");
- System.out.print("ID: " + id); //output
- System.out.println("str: " + str);
- }
- rss.close(); //cleanup
- stmt.close();
- conn.close();
- }catch(SQLException se){
- se.printStackTrace();
- }catch(Exception e){
- e.printStackTrace(); //handle errors for Class.forName
- }finally{
- try{
- if(stmt != null)
- stmt.close();
- }catch(SQLException se2){
- }
- try{
- if(conn != null)
- conn.close();
- }catch(SQLException se){
- se.printStackTrace();
- }
- }
- System.out.println("finished ending!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement