JDBC MySQL接続クラス(2007/11/13(火) 23:12:25)
MySQL Clusterの負荷テストの際に使用したMySQLを簡単に書くためのクラス。
勘違いもいっぱいありそうやけど、初めてのJDBC。
import java.sql.*; public class MySQL { private String url = "localhost"; private int prt = 3306; private String usr = "root"; private String pwd = "password"; private String dbn = ""; private Connection con = null; private Statement stmt = null; private ResultSet resultSet = null; private int updateCount = -1; MySQL(){ try{ Class.forName("com.mysql.jdbc.Driver"); }catch(ClassNotFoundException e){ e.printStackTrace(); } } MySQL(String url, int port, String usr, String pwd, String dbn){ this(); this.url = url; this.prt = prt; this.usr = usr; this.pwd = pwd; this.dbn = dbn; } public int getUpdateCount(){ return this.updateCount; } public ResultSet getResultSet(){ return this.resultSet; } public boolean connect(){ try{ // データベースへ接続 con = DriverManager.getConnection("jdbc:mysql://" + url + ":" + prt + "/" + dbn + "?useUnicode=true", usr, pwd); return true; }catch(SQLException e){ e.printStackTrace(); } return false; } public boolean deconnect(){ try{ if(this.con != null){ this.con.rollback(); this.con.rollback(); } if(this.stmt != null){ this.stmt.close(); } if(this.con != null){ this.con.close(); } return true; }catch(SQLException se){ se.printStackTrace(); } return false; } public boolean startTransaction(){ try{ this.stmt = this.con.createStatement(); this.con.setAutoCommit(false); return true; }catch(SQLException e){ try{ if(this.con != null){ this.con.rollback(); } }catch(SQLException se){ se.printStackTrace(); } e.printStackTrace(); } return false; } public boolean commit(){ try{ if(this.con != null){ this.con.commit(); } return true; }catch(SQLException e){ e.printStackTrace(); } return false; } public boolean rollback(){ try{ if(this.con != null){ this.con.rollback(); } return true; }catch(SQLException e){ e.printStackTrace(); } return false; } public boolean runExecute(String sql) throws SQLException{ //System.out.println("SQL : " + sql); boolean isResult = this.stmt.execute(sql); int rowCount = this.stmt.getUpdateCount(); //更新系クエリ結果 if(rowCount >= 0) { this.updateCount = rowCount; this.resultSet = null; return false; } //Select系クエリ結果 else { this.updateCount = -1; this.resultSet = this.stmt.getResultSet(); return true; } } public void addBatch(String sql) throws SQLException{ //System.out.println("SQL : " + sql); this.stmt.addBatch(sql); } public int[] runBatch() throws SQLException{ return this.stmt.executeBatch(); } public int runUpdate(String sql) throws SQLException{ //System.out.println("SQL : " + sql); this.updateCount = this.stmt.executeUpdate(sql); this.resultSet = null; return this.updateCount; } public ResultSet runQuery(String sql) throws SQLException{ //System.out.println("SQL : " + sql); this.updateCount = -1; this.resultSet = this.stmt.executeQuery(sql); return this.resultSet; } public static void printResultSet(ResultSet rs) throws SQLException{ ResultSetMetaData md = rs.getMetaData(); //カラム writeLine(md); for(int i = 1; i <= md.getColumnCount(); i++){ String col = md.getColumnName(i); System.out.print("|"); System.out.print(col); for(int j = 0; j < md.getColumnDisplaySize(i) - col.toCharArray().length; j++){ System.out.print(" "); } } System.out.println("|"); writeLine(md); //データ while(rs.next()){ for(int i = 1; i <= md.getColumnCount(); i++){ String val = rs.getString(md.getColumnName(i)); System.out.print("|" + val); for(int j = 0; j < md.getColumnDisplaySize(i) - val.toCharArray().length; j++){ System.out.print(" "); } } System.out.println("|"); } writeLine(md); System.out.println(""); } public static void writeLine(ResultSetMetaData md) throws SQLException{ String line = "+"; for(int i = 1; i <= md.getColumnCount(); i++){ for(int j = 0; j < md.getColumnDisplaySize(i); j++){ line += "-"; } line += "+"; } System.out.println(line); } public static int getRowSize(ResultSet rs) throws SQLException{ int row = rs.getRow(); rs.last(); int size = rs.getRow(); if(row == 0){ rs.first(); } else{ rs.absolute(row); } return size; } }