Advertisement
Guest User

Untitled

a guest
May 4th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.18 KB | None | 0 0
  1. package BazaDanych;
  2.  
  3. import java.sql.*;
  4.  
  5. public class DAO
  6. {
  7.     private Connection conn = null;
  8.     private String userName;
  9.     private String password;
  10.     private String address;
  11.     private String database;
  12.     private String prefix;
  13.     private int count;
  14.    
  15.     public DAO(String userName, String password, String address, String database, String prefix)
  16.     {
  17.         this.userName = userName;
  18.         this.password = password;
  19.         this.address = address;
  20.         this.database = database;
  21.         this.prefix = prefix;
  22.         this.count = 0;
  23.     }
  24.    
  25.     public DAO()
  26.     {
  27.         this("root", "php", "localhost", "lekarz24", "lekarz24");
  28.     }
  29.    
  30.     public void connect() throws Exception
  31.     {
  32.         String url = "jdbc:mysql://" + address + "/" + database;
  33.         Class.forName ("com.mysql.jdbc.Driver").newInstance ();
  34.         conn = DriverManager.getConnection (url, userName, password);
  35.     }
  36.     public ResultSet resultQuery(String sql) throws SQLException
  37.     {
  38.         Statement s = conn.createStatement();
  39.         ResultSet r = s.executeQuery(sql);
  40.         //s.close();
  41.         count++;
  42.         return r;
  43.     }
  44.     public int executeQuery(String sql) throws SQLException
  45.     {
  46.         Statement s = conn.createStatement();
  47.         int c = s.executeUpdate(sql);
  48.         s.close();
  49.         count++;
  50.         return c;
  51.     }
  52.     public void close()
  53.     {
  54.         try {
  55.             conn.close();
  56.         }
  57.         catch(Exception e) {
  58.            
  59.         }
  60.     }
  61.     public String getPrefix() {
  62.         return prefix;
  63.     }
  64.     public int getCount() {
  65.         return count;
  66.     }
  67.    
  68.     public void tr() throws SQLException
  69.     {
  70.         Statement s = conn.createStatement();
  71.         ResultSet r = s.executeQuery("SELECT * FROM " + prefix + "_lekarze");
  72.        
  73.         while (r.next()) {
  74.             System.out.println(r.getInt("Specjalizacja"));
  75.         }
  76.         r.close();
  77.     }
  78.     /*  try
  79.         {
  80.             String userName = "root";
  81.             String password = "php";
  82.             String url = "jdbc:mysql://localhost/lekarz24";
  83.             Class.forName ("com.mysql.jdbc.Driver").newInstance ();
  84.             conn = DriverManager.getConnection (url, userName, password);
  85.             System.out.println ("Database connection established");
  86.         }
  87.         catch (Exception e)
  88.         {
  89.             System.err.println ("Cannot connect to database server");
  90.             System.err.println (e.getMessage());
  91.         }
  92.  
  93.     }*/
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement