SHARE
TWEET

Untitled

a guest Jun 5th, 2017 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package pizzaShop;
  7.  
  8. /**
  9.  *
  10.  * @author pawel
  11.  */
  12.  
  13. import java.io.BufferedReader;
  14. import java.sql.Connection;
  15. import java.io.IOException;
  16. import java.sql.SQLException;
  17. import java.io.InputStreamReader;
  18. import java.sql.DriverManager;
  19. import java.sql.DatabaseMetaData;
  20. import java.sql.ResultSet;
  21. import java.sql.ResultSetMetaData;
  22.  
  23. import java.sql.Statement;
  24.  
  25. public class mySQLClient {
  26.    
  27.     String q, query, base, protocol, url, jdbc = "jdbc:mysql://", driver;
  28.     String[] splitQuery;
  29.     Connection connection;
  30.     Statement statement;
  31.     BufferedReader br;
  32.  
  33.     mySQLClient(String protocol, String base, String driver) {
  34.         this.base = base;
  35.         this.protocol = protocol;
  36.         this.driver = driver;
  37.         url = jdbc + protocol + "/" + base + "/";
  38.         try {
  39.             Class.forName(driver);
  40.             connection = DriverManager.getConnection(url,"root","");
  41.         } catch (ClassNotFoundException e) {
  42.             System.out.println("ClassNotFoundException");
  43.         } catch (SQLException e) {
  44.             System.out.println("SQLException");
  45.         }
  46.     }
  47.  
  48.     void setUrl(String protocol, String base) {
  49.         url = jdbc + protocol + "/" + base + "/";
  50.     }
  51.  
  52.     public void dmlQuery(String query) {
  53.         try {
  54.             statement = connection.createStatement();
  55.             statement.executeUpdate(query);
  56.             statement.close();
  57.         } catch (SQLException e) {
  58.             // TODO Auto-generated catch block
  59.             e.printStackTrace();
  60.         }
  61.         //System.out.println("OK");
  62.     }
  63.        
  64.     public void select(String query) {
  65.         try {
  66.             statement = connection.createStatement();
  67.             ResultSet resultSet = statement.executeQuery(query);
  68.             ResultSetMetaData metaData = resultSet.getMetaData();
  69.             while (resultSet.next()) {
  70.                 for(int i=0; i<metaData.getColumnCount(); ++i) {
  71.                     System.out.print(resultSet.getString(i+1)+" ");
  72.                 }
  73.                 System.out.println("");
  74.             }
  75.         } catch (SQLException e) {
  76.             // TODO Auto-generated catch block
  77.             e.printStackTrace();
  78.         }
  79.     }
  80.  
  81.         public boolean inTabel(String query) {
  82.                 this.query = query;
  83.         try {
  84.             statement = connection.createStatement();
  85.             ResultSet resultSet = statement.executeQuery(query);
  86.             if (resultSet.next()) {
  87.                             return true;
  88.             }
  89.         } catch (SQLException e) {
  90.             // TODO Auto-generated catch block
  91.             e.printStackTrace();
  92.         }
  93.                 return false;
  94.     }
  95.  
  96.         public void useDatabase(String base) {
  97.         this.base = base;
  98.         setUrl(protocol,base);
  99.         try {
  100.             connection = DriverManager.getConnection(url,"root","");
  101.         } catch (SQLException e) {
  102.             // TODO Auto-generated catch block
  103.             e.printStackTrace();
  104.         }
  105.     }
  106.  
  107.         public void showTables() {
  108.         DatabaseMetaData metaData;
  109.         try {
  110.             metaData = connection.getMetaData();
  111.             ResultSet resultSet = metaData.getTables(null, null, "%", null);
  112.             while (resultSet.next()) {
  113.                 System.out.println(resultSet.getString("TABLE_NAME"));
  114.             }
  115.         } catch (SQLException e) {
  116.             // TODO Auto-generated catch block
  117.             e.printStackTrace();
  118.         }
  119.     }
  120.  
  121.         public void describeTable(String tabel) {
  122.         try {
  123.             statement = connection.createStatement();
  124.             ResultSet resultSet = statement.executeQuery("SELECT * FROM "+tabel);
  125.             ResultSetMetaData metaData = resultSet.getMetaData();
  126.             for(int i = 0; i < metaData.getColumnCount(); ++i) {
  127.                 System.out.println(metaData.getColumnName(i+1));
  128.             }
  129.         } catch (SQLException e) {
  130.             // TODO Auto-generated catch block
  131.             e.printStackTrace();
  132.         }
  133.     }
  134.  
  135.         public String getField(String tabel, String userID, String column) {
  136.         try {
  137.             statement = connection.createStatement();
  138.                         query = "SELECT * FROM "+tabel+" WHERE UserID='"+userID+"';";
  139.             ResultSet resultSet = statement.executeQuery(query);
  140.             ResultSetMetaData metaData = resultSet.getMetaData();
  141.                         // 4 polem jest Password
  142.                         if (resultSet.next()) {
  143.                             return (String)resultSet.getString(4);
  144.             }
  145.         } catch (SQLException e) {
  146.             // TODO Auto-generated catch block
  147.             e.printStackTrace();
  148.         }
  149.                 return "";
  150.     }
  151.        
  152. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top