Advertisement
Guest User

Untitled

a guest
Oct 5th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1.  
  2.  
  3. import java.sql.*;
  4.  
  5. public class pSQL {
  6.     //LP version
  7.     public final static String MYSQL_HOST = "127.0.0.1";
  8.     public final static String MYSQL_DBNAME = "bili";
  9.     public final static String MYSQL_USER = "root";
  10.     public final static String MYSQL_PASS = "NyaNya";
  11.  
  12.  
  13.     static {
  14.         try {
  15.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  16.         } catch (Exception e) {
  17.             e.printStackTrace(System.out);
  18.         }
  19.     }
  20.  
  21.     public ResultSet rset;
  22.     public String errmsg = "none.";
  23.     public PreparedStatement ps;
  24.     private Connection ction;
  25.     private Statement statement;
  26.     private ResultSetMetaData rsmd;
  27.  
  28.     public boolean connect(String Host, String dbName, String User, String Pass) throws SQLException {
  29.         String url = "jdbc:mysql://" + Host + "/" + dbName + "?user=" + User + "&password=" + Pass + "&useUnicode=true&characterEncoding=utf8";//ָ������ ���PreparedStatement����
  30.         ction = DriverManager.getConnection(url);
  31.         statement = ction.createStatement();
  32.         return ction != null && statement != null;
  33.     }
  34.  
  35.     public boolean connect() throws SQLException {
  36.         return connect(MYSQL_HOST, MYSQL_DBNAME, MYSQL_USER, MYSQL_PASS);
  37.     }
  38.     public PreparedStatement pre(String sql) throws SQLException {
  39.         if (ction == null) this.connect();
  40.         ps = ction.prepareStatement(sql);
  41.         return ps;
  42.     }
  43.  
  44.     public PreparedStatement pre() {
  45.         return ps;
  46.     }
  47.  
  48.     public int exec() throws SQLException {
  49.         return ps.executeUpdate();
  50.     }
  51.  
  52.     public ResultSet query() throws SQLException {
  53.         rset = ps.executeQuery();
  54.         rsmd = rset.getMetaData();
  55.         return rset;
  56.     }
  57.  
  58.     public ResultSet rset() {
  59.         return rset;
  60.     }
  61.  
  62.     public boolean exec(String line) throws SQLException {
  63.         return statement.execute(line);
  64.     }
  65.  
  66.     public ResultSet query(String line) throws SQLException {
  67.         rset = statement.executeQuery(line);
  68.         rsmd = rset.getMetaData();
  69.         return rset;
  70.     }
  71.  
  72.     public int tcount() {
  73.         if (rsmd == null) return -1;
  74.         try {
  75.             return rsmd.getColumnCount();
  76.         } catch (Exception e) {
  77.             return -2;
  78.         }
  79.     }
  80.  
  81.     public int count() throws SQLException {
  82.         int cou = 0;
  83.         while (rset.next()) {
  84.             cou++;
  85.         }
  86.         return cou;
  87.     }
  88.  
  89.     public boolean isconnected() {
  90.         return !(ction == null);
  91.     }
  92.  
  93.     public void close() {
  94.         try {
  95.             ction = null;
  96.             rset.close();
  97.             statement.close();
  98.             ction.close();
  99.  
  100.         } catch (Exception e) {
  101.             //e.printStackTrace(System.out);
  102.         }
  103.     }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement