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 bank;
- /**
- *
- * @author Piotr- DELL
- */
- import java.sql.*;
- import java.util.ArrayList;
- public class DBOperations {
- /**
- * Connects to the DB server.
- * @param local
- * @return Connection
- * @throws SQLException
- */
- public static Connection connect( boolean local) throws SQLException{
- Connection connection = null;
- String username;
- String password;
- if(local){
- //username="terry1996";
- //password="YES";
- username="root";
- password="";
- connection = DriverManager.getConnection(
- //"jdbc:oracle:thin:@localhost:1521/xe", username, //moj adres IP
- "jdbc:mysql://127.0.0.1:3306/bank", username, //moj adres IP
- password);//open the connection
- }
- else{
- username="pwr_16_17_L_015226029";
- System.out.println("jest konekcja pozalokalna");
- password="blue96011103515";
- connection = DriverManager.getConnection(
- "jdbc:oracle:thin:@156.17.43.90:1521:xe", username, //serwer Pwr
- password);//open the connection
- }
- return connection;
- }
- /**
- * Checks if the ORACLE driver is available. If not, throws an exception
- * @throws ClassNotFoundException
- */
- public static void checkLib() throws ClassNotFoundException{
- Class.forName("oracle.jdbc.driver.OracleDriver");
- }
- /**
- * Executes a query using the established connection.
- * @param connection
- * @param query
- * @return an ArrayList<St`ring> that contains the output of the query.
- * @throws Exception
- */
- public static ArrayList<String> executeQuery(Connection connection,String query)throws Exception{
- ArrayList<String> outRes=new ArrayList<String>();
- Statement stmt = null;
- ResultSet rs=null;
- ResultSetMetaData rsmd=null;
- stmt = connection.createStatement();
- rs = stmt.executeQuery(query);
- rsmd = rs.getMetaData();
- int colNum = rsmd.getColumnCount();
- StringBuilder tmp = null;
- while(rs.next()){//Iterate over each row of the result
- tmp = new StringBuilder();
- for(int i=1;i<=colNum;i++){//Iterate over each column of the result
- tmp.append(rs.getString(i));//get the column-specific data in the form of a string
- tmp.append("\t");
- }
- outRes.add(tmp.toString());
- }
- return outRes;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement