Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.87 KB | None | 0 0
  1. public class DB {
  2.         private  ResultSet rs;
  3.     private  Connection con;
  4.     private  Statement stmt;
  5.     private  String url;
  6.  
  7.     public DB() {
  8.        
  9.     }
  10.  
  11.     public  void dbconnect() {
  12.         try {
  13.             Class.forName("com.mysql.jdbc.Driver");
  14.      
  15.             url="jdbc:mysql://192.168.20.98:3306/games";
  16.             this.con=DriverManager.getConnection(url,"user","pass");
  17.  
  18.  
  19.  
  20.  
  21.         } catch (SQLException ex) {
  22.             Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
  23.         } catch (ClassNotFoundException ex) {
  24.             Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
  25.         }
  26.  
  27.  
  28.     }
  29.  
  30.     public  ResultSet query(String sql) {
  31.         try{
  32.         close();
  33.         }catch(Exception e){}
  34.         this.dbconnect();
  35.         try {
  36.             stmt = con.createStatement();
  37.             rs=stmt.executeQuery(sql);
  38.  
  39.         } catch (SQLException ex) {
  40.             Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
  41.         }
  42.  
  43.  
  44.         return rs;
  45.     }
  46.  
  47.     public  int update(String sql) {
  48.         int result=0;
  49.         try{
  50.         close();
  51.         }catch(Exception e){}
  52.         dbconnect();
  53.         try{
  54.              stmt = con.createStatement();
  55.             result=stmt.executeUpdate(sql);
  56.  
  57.  
  58.         }catch(SQLException ex) {
  59.            Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
  60.         }
  61.  
  62.         return result;
  63.     }
  64.  
  65.     public void close() {
  66.         try {
  67.             rs.close();
  68.             stmt.close();
  69.             con.close();
  70.         } catch (SQLException ex) {
  71.             Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
  72.         }
  73.  
  74.  
  75.     }
  76. }
  77.  
  78. // and to query i use
  79.  
  80. DB db=new DB();
  81. ResultSet rs=db.query("some query");
  82. while(rs.next()) {
  83.  
  84. ...
  85.  
  86. }
  87. db.close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement