package adatbazis; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public class Mysql { String host; String port; String database; String user; String pass; Mysql() { initProperties(); } public void initProperties() { try { tryInitProperties(); } catch (FileNotFoundException ex) { System.err.println("A fájl nem található!"); } catch (IOException ex) { System.err.println("Hiba az olvasás során!"); } } public void tryInitProperties() throws FileNotFoundException, IOException { Properties pro = new Properties(); InputStream fis = new FileInputStream("adatbazisswing.properties"); pro.load(fis); this.host = pro.getProperty("host"); this.port = pro.getProperty("port"); this.database = pro.getProperty("database"); this.user = pro.getProperty("user"); this.pass = pro.getProperty("pass"); } public void checkMysqlDriver() { try { tryCheckMysqlDriver(); }catch(ClassNotFoundException ex) { System.err.println("Az osztály nem található!"); }catch(InstantiationException ex) { System.err.println("Hiba a példányosítás során!"); }catch(IllegalAccessException ex) { System.err.println("Hozáférésre nem jogosult!"); } } public void tryCheckMysqlDriver() throws ClassNotFoundException, InstantiationException, IllegalAccessException { Class.forName("org.gjt.mm.mysql.Driver").newInstance(); } public Connection connect() { Connection conn = null; try { conn = tryConnect(); } catch (SQLException ex) { System.err.println("Hiba az adatbázishoz kapcsolódás során!"); } return conn; } public Connection tryConnect() throws SQLException { checkMysqlDriver(); String url = "jdbc:mysql://" + host + ":" + port + "/" + database; System.out.println(url + " " + user + " " + pass); Connection conn = DriverManager.getConnection(url, user, pass); //DriverManager.setLogWriter(new PrintWriter(System.err)); return conn; } public void close(Connection conn) { try { tryClose(conn); } catch (SQLException ex) { System.err.println("Hiba az adatbázis bezárása során!"); } } public void tryClose(Connection conn) throws SQLException { conn.close(); } public ResultSet query(Connection conn, String sql) { ResultSet rs = null; try { rs = tryQuery(conn, sql); } catch (SQLException ex) { System.err.println("Hiba a lekérdezés során"); } return rs; } public ResultSet tryQuery(Connection conn, String sql) throws SQLException { Statement stmt = conn.createStatement(); return stmt.executeQuery(sql); } public void beszur(Connection con, String sql) { try { tryBeszur(con, sql); } catch (SQLException ex) { System.err.println("Hiba az SQL végrehajtása során!"); } } public void tryBeszur(Connection con, String sql) throws SQLException { PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, "Bereg József"); ps.setString(2, "Szolnok"); System.out.println(ps.toString()); ps.executeUpdate(); } }