想要通过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);
```
若是连接成功变更在后台看到数据啦。