Advertisement
Guest User

Untitled

a guest
Apr 25th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1. package utk;
  2.  
  3. import java.sql.*;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.SQLException;
  7.  
  8. public class JDBC {
  9.  
  10.     public static String[] pytanie = new String[40];
  11.     public static int[] id = new int[40];
  12.     public static int[] poprawna = new int[40];
  13.     public static int[] nrTestu = new int[40];
  14.     public static String[] odpA = new String[40];
  15.     public static String[] odpB = new String[40];
  16.     public static String[] odpC = new String[40];
  17.     public static String[] odpD = new String[40];
  18.     public static String[] zdjecie = new String[40];
  19.     public static String[] kategoria = new String[40];
  20.  
  21.     public static int i = 0;
  22.  
  23.     public static void polacz() {
  24.  
  25.         String polaczenieURL = "jdbc:mysql://192.168.2.51/UTK";
  26.         String user = "root";
  27.         String passwd = "zaq1@WSX";
  28.         String query = "Select * FROM odpowiedzi";
  29.  
  30.         Connection conn = null;
  31.  
  32.         try {
  33.  
  34.             conn = DriverManager.getConnection(polaczenieURL, user, passwd);
  35.  
  36.             Class.forName("com.mysql.jdbc.Driver");
  37.  
  38.             Statement stmt = conn.createStatement();
  39.             ResultSet rs = stmt.executeQuery(query);
  40.  
  41.             while (rs.next() && i < 40) {
  42.                 pobierz40(rs, i);
  43.                 i++;
  44.             }
  45.  
  46.             conn.close();
  47.         }
  48.  
  49.         catch (ClassNotFoundException wyjatek) {
  50.             System.out.println("Problem ze sterownikiem");
  51.         }
  52.  
  53.         catch (SQLException wyjatek) {
  54.             System.out.println("SQLException: " + wyjatek.getMessage());
  55.             System.out.println("SQLState: " + wyjatek.getSQLState());
  56.             System.out.println("VendorError: " + wyjatek.getErrorCode());
  57.         }
  58.  
  59.     }
  60.  
  61.     static void pobierz40(ResultSet rs, int i) {
  62.         try {
  63.             id[i] = rs.getInt(1);
  64.             pytanie[i] = rs.getString(2);
  65.             odpA[i] = rs.getString(3);
  66.             odpB[i] = rs.getString(4);
  67.             odpC[i] = rs.getString(5);
  68.             odpD[i] = rs.getString(6);
  69.             poprawna[i] = rs.getInt(7);
  70.             nrTestu[i] = rs.getInt(8);
  71.             zdjecie[i] = rs.getString(9);
  72.             kategoria[i] = rs.getString(10);
  73.  
  74.         } catch (SQLException e) {
  75.             e.printStackTrace();
  76.         }
  77.     }
  78.  
  79.  
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement