import java.sql.*; class Mysql { String host = "localhost"; String port = "3306"; String db = "sargazrt"; String user = "sargazrt"; String pass = "titok"; 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 { Connection conn = null; String url = "jdbc:mysql://" + host + ":" + port + "/"+ db; conn = DriverManager.getConnection(url, user, pass); 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 { ResultSet rs = null; Statement stmt = conn.createStatement(); return stmt.executeQuery(sql); } } class Program01 { public static void main(String[] args) { System.out.println("MySQL kapcsolódás..."); Mysql mysql = new Mysql(); Connection conn = mysql.connect(); String sql = "select * from Dolgozok"; ResultSet rs = mysql.query(conn, sql); try { while(rs.next()) { System.out.printf("%s %s\n", rs.getString("nev"), rs.getString("telepules") ); } }catch(SQLException ex) { System.err.println("Hiba az adatok kinyerése során!"); } mysql.close(conn); } }