Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package BazaDanych;
- import java.sql.*;
- public class DAO
- {
- private Connection conn = null;
- private String userName;
- private String password;
- private String address;
- private String database;
- private String prefix;
- private int count;
- public DAO(String userName, String password, String address, String database, String prefix)
- {
- this.userName = userName;
- this.password = password;
- this.address = address;
- this.database = database;
- this.prefix = prefix;
- this.count = 0;
- }
- public DAO()
- {
- this("root", "php", "localhost", "lekarz24", "lekarz24");
- }
- public void connect() throws Exception
- {
- String url = "jdbc:mysql://" + address + "/" + database;
- Class.forName ("com.mysql.jdbc.Driver").newInstance ();
- conn = DriverManager.getConnection (url, userName, password);
- }
- public ResultSet resultQuery(String sql) throws SQLException
- {
- Statement s = conn.createStatement();
- ResultSet r = s.executeQuery(sql);
- //s.close();
- count++;
- return r;
- }
- public int executeQuery(String sql) throws SQLException
- {
- Statement s = conn.createStatement();
- int c = s.executeUpdate(sql);
- s.close();
- count++;
- return c;
- }
- public void close()
- {
- try {
- conn.close();
- }
- catch(Exception e) {
- }
- }
- public String getPrefix() {
- return prefix;
- }
- public int getCount() {
- return count;
- }
- public void tr() throws SQLException
- {
- Statement s = conn.createStatement();
- ResultSet r = s.executeQuery("SELECT * FROM " + prefix + "_lekarze");
- while (r.next()) {
- System.out.println(r.getInt("Specjalizacja"));
- }
- r.close();
- }
- /* try
- {
- String userName = "root";
- String password = "php";
- String url = "jdbc:mysql://localhost/lekarz24";
- Class.forName ("com.mysql.jdbc.Driver").newInstance ();
- conn = DriverManager.getConnection (url, userName, password);
- System.out.println ("Database connection established");
- }
- catch (Exception e)
- {
- System.err.println ("Cannot connect to database server");
- System.err.println (e.getMessage());
- }
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement