Advertisement
Guest User

Untitled

a guest
Apr 20th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.sql.*;
  4.  
  5.  
  6. public class Connector {
  7.  
  8.     private static final String url = "jdbc:mysql://localhost:3306/polynom"; //define database
  9.     private static final String user = "root";
  10.     private static final String password = "110895aa";
  11.  
  12.     protected static int degree;
  13.     protected static int j;
  14.     protected static int poly;
  15.     protected static String type;
  16.  
  17.  
  18.     // JDBC variables for opening and managing connection
  19.     protected static Connection con;
  20.     protected static Statement stmt;
  21.     protected static ResultSet rs;
  22.     protected static int connectionTimes=0;
  23.  
  24.  
  25.     protected static String polyType;
  26.     protected static int[] arrayOfPolyList = new int[20];
  27.     protected static int arrayOfPolyListSize =0;
  28.     private int i;
  29.  
  30.  
  31.  
  32.     void getConnection(String query){
  33.         i=0;
  34.  
  35.         try {
  36.             // opening database connection to MySQL server
  37.             con = DriverManager.getConnection(url, user, password);
  38.  
  39.             // getting Statement object to execute query
  40.             stmt = con.createStatement();
  41.  
  42.             // executing SELECT query
  43.             rs = stmt.executeQuery(query);
  44.  
  45.             //Don't do anything while connect at first!
  46.             if (connectionTimes > 0) {
  47.  
  48.                 while(rs.next()) {
  49.                     arrayOfPolyList[i] = rs.getInt("poly");
  50.                     i++;
  51.                 }
  52.                 arrayOfPolyListSize =i; //getting size of the array
  53.             }
  54.  
  55.  
  56.         } catch (SQLException sqlEx) {
  57.             sqlEx.printStackTrace();
  58.         } finally {
  59.             //close connection ,stmt and resultset here
  60.             try { con.close(); } catch(SQLException se) { /*can't do anything */ }
  61.             try { stmt.close(); } catch(SQLException se) { /*can't do anything */ }
  62.             try { rs.close(); } catch(SQLException se) { /*can't do anything */ }
  63.         }
  64.         connectionTimes++;
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement