Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DB {
- private ResultSet rs;
- private Connection con;
- private Statement stmt;
- private String url;
- public DB() {
- }
- public void dbconnect() {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- url="jdbc:mysql://192.168.20.98:3306/games";
- this.con=DriverManager.getConnection(url,"user","pass");
- } catch (SQLException ex) {
- Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public ResultSet query(String sql) {
- try{
- close();
- }catch(Exception e){}
- this.dbconnect();
- try {
- stmt = con.createStatement();
- rs=stmt.executeQuery(sql);
- } catch (SQLException ex) {
- Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
- }
- return rs;
- }
- public int update(String sql) {
- int result=0;
- try{
- close();
- }catch(Exception e){}
- dbconnect();
- try{
- stmt = con.createStatement();
- result=stmt.executeUpdate(sql);
- }catch(SQLException ex) {
- Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
- }
- return result;
- }
- public void close() {
- try {
- rs.close();
- stmt.close();
- con.close();
- } catch (SQLException ex) {
- Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- // and to query i use
- DB db=new DB();
- ResultSet rs=db.query("some query");
- while(rs.next()) {
- ...
- }
- db.close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement