Java 存取資料庫是很常見的一種應用,紀錄一下Java存取 MySQL範例說明 ,這裏使用Connector/J來連接MySQL,所以要先下載Connector/J(http://www.mysql.com/downloads/connector/j/),下載完成後解壓縮,可以得要一個mysql-connector-java-5.1.17-bin.jar就是MySQL的JDBC-Driver了, 接著在Eclipse建立一個Java Project,簡易步驟如下:
建立Java Project
選好JRE版本後,按下Finish..建立完成
mysql-connector-java-5.1.17-bin.jar
Project建立完成後,再把mysql-connector-java-5.1.17-bin.jar加到Buildpath中
設定完成後,就可以開始實做了,在專案裡 new 一個Class,範例的名稱為mysql,程式碼和說明如下。
程式流程:
與建立DB連線->新增資料表->刪除資料表->新增資料表->新增一筆資料->查詢資料表->印出結果
public class mysql { public static void main(String[] args) throws Exception { //建立DB連線 jdbcmysql db = new jdbcmysql(); //建立Table db.createTable(); //刪除Table db.dropTable(); //建立Table db.createTable(); //寫入溫度計資料 db.insertTemperature("01010101", 28.0); //讀取溫度計資料 db.SelectTemperature(); } } //資料庫 //建立jdbc類別 class jdbcmysql { //連接object private Connection con = null; //Database objects //執行,傳入之sql為完整字串 private Statement stat = null; //結果集 private ResultSet rs = null; private PreparedStatement pst = null; //select:查詢溫度所有資料 private String selectTemperature2DBSQL = "select * from temperature "; //insert:執行sql,先利用?來做標示傳入變數之位置 private String insertTemperature2SQL = "insert into temperature(deviceid,time,value) " + "VALUES (?,default,?)"; //Drop table private String dropdbSQL = "DROP TABLE temperature "; //Create table private String createdbSQL = "CREATE TABLE temperature (" + " deviceid varchar(30) " + " , time timestamp " + " , value float(7,2))"; public jdbcmysql() { try { //註冊driver Class.forName("com.mysql.jdbc.Driver"); //取得connection //localhost是主機名,fcu_db是database名 //useUnicode=true&characterEncoding=Big5使用的編碼 con = DriverManager.getConnection( "jdbc:mysql://localhost/fcu_db?useUnicode=true&characterEncoding=Big5", "root",""); } catch(ClassNotFoundException e) { //sqlexception System.out.println("DriverClassNotFound :"+e.toString()); }catch(SQLException x) { System.out.println("Exception :"+x.toString()); } } //刪除Table, public void dropTable() { try { stat = con.createStatement(); stat.executeUpdate(dropdbSQL); } catch(SQLException e) { System.out.println("DropDB Exception :" + e.toString()); } finally { Close(); } } //建立table public void createTable() { try { stat = con.createStatement(); stat.executeUpdate(createdbSQL); } catch(SQLException e) { System.out.println("CreateDB Exception :" + e.toString()); } finally { Close(); } } //查詢溫度資料 public void SelectTemperature() { try { stat = con.createStatement(); rs = stat.executeQuery(selectTemperature2DBSQL); System.out.println("deviceid\t\ttime\t\tvalue"); while(rs.next()) { System.out.println(rs.getInt("deviceid")+"\t\t"+ rs.getString("time")+"\t\t"+rs.getString("value")); } } catch(SQLException e) { System.out.println("DropDB Exception :" + e.toString()); } finally { Close(); } } //新增溫度資料 public void insertTemperature( String id,double value) { try { pst = con.prepareStatement(insertTemperature2SQL); pst.setString(1,id); pst.setDouble(2, value); pst.executeUpdate(); } catch(SQLException e) { System.out.println("InsertDB Exception :" + e.toString()); } finally { Close(); } } //完整使用完資料庫後,記得要關閉所有Object //否則在等待Timeout時,可能會有Connection poor的狀況 private void Close() { try { if(rs!=null) { rs.close(); rs = null; } if(stat!=null) { stat.close(); stat = null; } if(pst!=null) { pst.close(); pst = null; } } catch(SQLException e) { System.out.println("Close Exception :" + e.toString()); } } }
執行結果:
deviceid time value 1010101 2013-01-24 14:42:11.0 28.00
沒有留言:
張貼留言