1. 程式人生 > >java 資料視覺化(二)通過jdbc訪問資料庫,在servlet上獲取資料庫資料

java 資料視覺化(二)通過jdbc訪問資料庫,在servlet上獲取資料庫資料

想要通過servlet獲取資料庫資料,首先需要建立jdbc 因為資料是通過無線感測傳到資料庫的,因此jdbc裡只有查詢操作,增刪改的同學可以自行新增。 程式碼中被註釋掉的部分用於測試。

  • main函式部分用於檢測是否連線上資料庫,並檢測是否能讀到資料,若是讀得到就在後臺列印
  • 建構函式部分用於建立jdbc類後,檢查是否能從資料庫讀到資料
package com.jdbc;

import com.jdbc.Data;
import java.sql.*;


import java.sql.Connection;
import java.sql.DriverManager;
import java.
sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; public class jdbc { final static String USERNAME = "root"; final static String PASSWORD = ""; final static String DRIVER = "com.mysql.jdbc.Driver"; final static String URL = "jdbc:mysql://localhost:3306/test1"; static Connection connection;
/*public static void main(String[] args) throws SQLException { try { Class.forName(DRIVER); System.out.println("Register driver success"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block System.out.println("Register driver failure"); } connection=getConnection(); String sql = "select * from testtable2t"; List<Data> result = getData(sql); int i = 0; for(Data a:result) { String aaa =(String)a.time; String bbb = (String)a.power; //*time[i] = aaa;*/
/* //*power1[i] = bbb;*//* System.out.println(aaa); System.out.println(bbb); i++; }}*/ public jdbc() throws SQLException { try { Class.forName("com.mysql.jdbc.Driver"); System.out.println("Register driver success"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("Register driver failure"); } connection=getConnection(); /* String sql = "select * from testtable2t"; List<Data> result = getData(sql); int i = 0; for(Data a:result) { String aaa =(String)a.time; String bbb = (String)a.power1; System.out.println(time[i]); power1[i]= (String)bbb; i++; }*/ } public static Connection getConnection() { try { connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); } catch (Exception e) { // TODO: handle exception System.out.println("Connection exception !"); } return connection; } public static ArrayList<Data> getData(String sql) throws SQLException{ ArrayList<Data> result = new ArrayList<Data>(); Statement statement=null; statement=connection.createStatement(); ResultSet rs = statement.executeQuery(sql); Data time = null; while(rs.next()) { time= new Data(); time.time=rs.getString("time"); time.power=rs.getString("pulse"); result.add(time); } return result; } }

其中用於接受資料的data類構造如下:

package com.jdbc;

public class Data {
    public String time;
    public String power;
    public void setTime(String time){
        this.time = time;
    }
    public String getTime(){
        return this.time;
    }
    public void setPower(String power){
        this.power = power;
    }
    public String getPower(){
        return this.power;
    }
}

jdbc建立好後,便可以在servlet中通過jdbc獲取資料庫資料了 servlet建立jdbc類獲取資料並列印,程式碼段如下:

       request.setCharacterEncoding("utf-8");
       ArrayList<Data> result = null;
        jdbc DataBase = null;
        try {
            DataBase = new jdbc();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        String sql = "select * from testtable2t";
        try {
            result = DataBase.getData(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println(result);
    ```
    若是連線成功變更在後臺看到資料啦。