Advertisement
Guest User

Untitled

a guest
Dec 9th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 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 bank;
  7. /**
  8.  *
  9.  * @author Piotr- DELL
  10.  */
  11. import java.sql.*;
  12. import java.util.ArrayList;
  13.  
  14. public class DBOperations {
  15.  
  16.     /**
  17.      * Connects to the DB server.
  18.          * @param local
  19.      * @return Connection
  20.      * @throws SQLException
  21.      */
  22.     public static Connection connect( boolean local) throws SQLException{
  23.         Connection connection = null;
  24.                
  25.                 String username;
  26.                 String password;
  27.                
  28.                 if(local){
  29.                     //username="terry1996";
  30.                     //password="YES";
  31.                     username="root";
  32.                     password="";
  33.                     connection = DriverManager.getConnection(
  34.                                 //"jdbc:oracle:thin:@localhost:1521/xe", username,   //moj adres IP
  35.                                 "jdbc:mysql://127.0.0.1:3306/bank", username,   //moj adres IP
  36.                 password);//open the connection
  37.                 }
  38.                 else{
  39.                     username="pwr_16_17_L_015226029";
  40.                     System.out.println("jest konekcja pozalokalna");
  41.                     password="blue96011103515";
  42.                     connection = DriverManager.getConnection(
  43.                 "jdbc:oracle:thin:@156.17.43.90:1521:xe", username, //serwer Pwr
  44.                 password);//open the connection
  45.                 }
  46.         return connection;
  47.     }
  48.     /**
  49.      * Checks if the ORACLE driver is available. If not, throws an exception
  50.      * @throws ClassNotFoundException
  51.      */
  52.     public static void checkLib() throws ClassNotFoundException{
  53.         Class.forName("oracle.jdbc.driver.OracleDriver");
  54.     }
  55.     /**
  56.      * Executes a query using the established connection.
  57.      * @param connection
  58.      * @param query
  59.      * @return an ArrayList<St`ring> that contains the output of the query.
  60.      * @throws Exception
  61.      */
  62.     public static ArrayList<String> executeQuery(Connection connection,String query)throws Exception{
  63.         ArrayList<String> outRes=new ArrayList<String>();
  64.         Statement stmt = null;
  65.         ResultSet rs=null;
  66.         ResultSetMetaData rsmd=null;
  67.        
  68.         stmt = connection.createStatement();
  69.         rs = stmt.executeQuery(query);
  70.         rsmd = rs.getMetaData();
  71.        
  72.         int colNum = rsmd.getColumnCount();
  73.         StringBuilder tmp = null;
  74.         while(rs.next()){//Iterate over each row of the result
  75.             tmp = new StringBuilder();
  76.             for(int i=1;i<=colNum;i++){//Iterate over each column of the result
  77.                 tmp.append(rs.getString(i));//get the column-specific data in the form of a string
  78.                 tmp.append("\t");
  79.             }
  80.            
  81.             outRes.add(tmp.toString());
  82.         }
  83.        
  84.        
  85.         return outRes;
  86.     }
  87.  
  88.    
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement