Guest User

Untitled

a guest
Mar 23rd, 2016
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.95 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6.  
  7. public class DB {
  8.  
  9.     public Connection conn = null;
  10.  
  11.     public DB() {
  12.         try {
  13.             Class.forName("com.mysql.jdbc.Driver");
  14.             String url = "jdbc:mysql://localhost:3306/landme";
  15.             conn = DriverManager.getConnection(url, "root", "");
  16.             System.out.println("conn built");
  17.         } catch (SQLException e) {
  18.             e.printStackTrace();
  19.         } catch (ClassNotFoundException e) {
  20.             e.printStackTrace();
  21.         }
  22.     }
  23.  
  24.     public ResultSet runSql(String sql) throws SQLException {
  25.         Statement sta = conn.createStatement();
  26.         return sta.executeQuery(sql);
  27.     }
  28.  
  29.     public boolean runSql2(String sql) throws SQLException {
  30.         Statement sta = conn.createStatement();
  31.         return sta.execute(sql);
  32.     }
  33.  
  34.     @Override
  35.     protected void finalize() throws Throwable {
  36.         if (conn != null || !conn.isClosed()) {
  37.             conn.close();
  38.         }
  39.     }
  40. }
Add Comment
Please, Sign In to add comment