数据库插入数据的类
package MySQL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class ChaRu {
//获取连接对象,返回连接对象
public static Connection getconn(){
Connection conn = null;
try{
conn = MySQL.DBUtil_lmtest01.getConnection();
}catch(SQLException e){
e.printStackTrace();
}
return conn;
}
//将数据插入到数据库中,一次将一个id的所有特征向量插入
//2个主键标识:id(帧的id) flag(手势类型)
//4个特征向量(15个数据):
static PreparedStatement ps = null;
static Connection conn = null;
public static void insert(Connection con,int number,long id,double[] data){//运动轨迹
int flag = 2;
conn = con;
double[] euler = new double[3];//手掌姿态
double[] w = new double[5];//手指弯曲度
double[] gm = new double[5];//手指弯曲度
double det;//运动轨迹
double fit;
for(int i=0;i<3;i++){
euler[i] = data[i];
}
for(int i=3;i<8;i++){
w[i-3] = data[i];
}
for(int i=8;i<13;i++){
gm[i-8] = data[i];
}
det = data[13];
fit = data[14];
try{
String sql = "insert into t_shoushi"
+ "(number,id,flag,euler1,euler2,euler3,w1,w2,w3,w4,w5,gm1,gm2,gm3,gm4,gm5,det,fit) "
+ "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
ps = conn.prepareStatement(sql);
ps.setLong(1, number);
ps.setLong(2, id);
ps.setLong(3, flag);
ps.setDouble(4, euler[0]);
ps.setDouble(5, euler[1]);
ps.setDouble(6, euler[2]);
ps.setDouble(7, w[0]);
ps.setDouble(8, w[1]);
ps.setDouble(9, w[2]);
ps.setDouble(10, w[3]);
ps.setDouble(11, w[4]);
ps.setDouble(12, gm[0]);
ps.setDouble(13, gm[1]);
ps.setDouble(14, gm[2]);
ps.setDouble(15, gm[3]);
ps.setDouble(16, gm[4]);
ps.setDouble(17, det);
ps.setDouble(18, fit);
int count = ps.executeUpdate();
System.out.println(count);
}catch(Exception e){
e.printStackTrace();
}
}
/*
* 数据库插入数据(连接对象,手势编号,数据)
*/
public static void insert(Connection con,int number,double[] data){//运动轨迹
int flag = 4;
conn = con;
double[] euler = new double[3];//手掌姿态
double[] w = new double[5];//手指弯曲度
double[] gm = new double[5];//手指弯曲度
double det;//运动轨迹
double fit;
for(int i=0;i<3;i++){
euler[i] = data[i];
}
for(int i=3;i<8;i++){
w[i-3] = data[i];
}
for(int i=8;i<13;i++){
gm[i-8] = data[i];
}
det = data[13];
fit = data[14];
try{
String sql = "insert into t_shoushi"
+ "(number,flag,euler1,euler2,euler3,w1,w2,w3,w4,w5,gm1,gm2,gm3,gm4,gm5,det,fit) "
+ "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
ps = conn.prepareStatement(sql);
ps.setLong(1, number);
ps.setLong(2, flag);
ps.setDouble(3, euler[0]);
ps.setDouble(4, euler[1]);
ps.setDouble(5, euler[2]);
ps.setDouble(6, w[0]);
ps.setDouble(7, w[1]);
ps.setDouble(8, w[2]);
ps.setDouble(9, w[3]);
ps.setDouble(10, w[4]);
ps.setDouble(11, gm[0]);
ps.setDouble(12, gm[1]);
ps.setDouble(13, gm[2]);
ps.setDouble(14, gm[3]);
ps.setDouble(15, gm[4]);
ps.setDouble(16, det);
ps.setDouble(17, fit);
int count = ps.executeUpdate();
System.out.println(count);
}catch(Exception e){
e.printStackTrace();
}
}
public static void insert(Connection con,int number,int id,int[] k,int flag){//运动轨迹
conn = con;
try{
String sql = "update t_shoushi set k1=?,k2=?,k3=?,k4=? where number=? and id=? and flag=?;";
ps = conn.prepareStatement(sql);
ps.setLong(1, k[0]);
ps.setLong(2, k[1]);
ps.setDouble(3,k[2]);
ps.setDouble(4,k[3]);
ps.setDouble(5,number);
ps.setDouble(6,id);
ps.setDouble(7,flag);
int count = ps.executeUpdate();
System.out.println(count);
}catch(Exception e){
e.printStackTrace();
}
}
public static void close(){
MySQL.DBUtil.close(conn, ps);
}
//最开始写的插入方法模板,包括了获取对象和插入数据功能
/*原始insert
public static void insert(){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
//获取连接
conn = MySQL.DBUtil_lmtest.getConnection();
//获取预编译的数据库操作对象
String sql = "select euler1,euler2,euler3 from t_szzt where id=?";
ps = conn.prepareStatement(sql);
ps.setString(1, "1");
rs = ps.executeQuery();
while(rs.next()){
System.out.println("euler1:"+rs.getDouble("euler1"));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//释放资源
MySQL.DBUtil.close(conn, ps, rs);
}
}
*/
}
插入用到的工具类
package MySQL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtil_lmtest01 {
private DBUtil_lmtest01(){}
static{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException{
return DriverManager.getConnection("jdbc:mysql://localhost:3306/lmtest01", "root", "xiaoyi");
}
public static void close(Connection conn,Statement ps){
if(ps != null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
查找
package MySQL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class ChaZhao {
public static Connection getconn(){//获取连接对象,返回连接对象
Connection conn = null;
try{
conn = MySQL.DBUtil_lmtest01.getConnection();
}catch(SQLException e){
e.printStackTrace();
}
return conn;
}
static Connection conn = null;
static PreparedStatement ps = null;
static ResultSet rs = null;
public static ArrayList<float[]> select(Connection con){
ArrayList<float[]> dataSet=new ArrayList<float[]>();
try {
//获取连接
conn = con;
//获取预编译的数据库操作对象
//String sql = "select euler1,euler2,euler3 from t_shoushi where flag = ?";//"
String sql = "select k4 from t_shoushi where flag = 4 and number<=70";
//String sql = "select w1,w2,w3,w4,w5 from t_shoushi";
//String sql = "select gm1,gm2,gm3,gm4,gm5 from t_shoushi";
//String sql = "select det,fit from t_shoushi";
ps = conn.prepareStatement(sql);
//ps.setString(1, "2");
rs = ps.executeQuery();
int num = 0;
while(rs.next()){
num++;
float[] data = new float[17];
data[0] = rs.getFloat("k4");
dataSet.add(data);
}
/*
while(rs.next()){
num++;
float[] data = new float[5];
data[0] = rs.getFloat("gm1");
data[1] = rs.getFloat("gm2");
data[2] = rs.getFloat("gm3");
data[3] = rs.getFloat("gm4");
data[4] = rs.getFloat("gm5");
dataSet.add(data);
}
/*
while(rs.next()){
num++;
float[] data = new float[5];
data[0] = rs.getFloat("det");
data[1] = rs.getFloat("fit");
dataSet.add(data);
}
*/
System.out.println(num);
} catch (Exception e) {
e.printStackTrace();
}
return dataSet;
}
/*
public static ArrayList<Integer> select1(Connection con){
ArrayList<Integer> dataSet=new ArrayList<Integer>();
try {
//获取连接
conn = con;
//获取预编译的数据库操作对象
//String sql = "select euler1,euler2,euler3 from t_shoushi where flag = ?";//"
String sql = "select "+
"id,"+
"?"+
"from t_shoushi where flag=? and number=1";
ps = conn.prepareStatement(sql);
ps.setString(1, "k1");
ps.setString(2, "1");
rs = ps.executeQuery();
int num = 0;
while(rs.next()){
dataSet.add((int)rs.getFloat("k1"));
num++;
}
System.out.println(num);
} catch (Exception e) {
e.printStackTrace();
}
return dataSet;
}
*/
public static void close(){
MySQL.DBUtil.close(conn, ps, rs);
}
}
HMM训练时所用的查找数据的方法
package MySQL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class ChaZhao_train {
public static Connection getconn(){//获取连接对象,返回连接对象
Connection conn = null;
try{
conn = MySQL.DBUtil_lmtest01.getConnection();
}catch(SQLException e){
e.printStackTrace();
}
return conn;
}
static Connection conn = null;
static PreparedStatement ps = null;
static ResultSet rs = null;
public static ArrayList<float[]> select(Connection con){
ArrayList<float[]> dataSet=new ArrayList<float[]>();
try {
//获取连接
conn = con;
//获取预编译的数据库操作对象
String sql = "select number,id,k1,k2,k3,k4 from t_shoushi where flag=4 and number>70";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
int num = 0;
while (rs.next()){
num++;
float[] data = new float[6];
data[0] = rs.getFloat("number");
data[1] = rs.getFloat("id");
data[2] = rs.getFloat("k1");
data[3] = rs.getFloat("k2");
data[4] = rs.getFloat("k3");
data[5] = rs.getFloat("k4");
dataSet.add(data);
}
System.out.println(num);
} catch (Exception e) {
e.printStackTrace();
}
return dataSet;
}
public static void close(){
MySQL.DBUtil.close(conn, ps, rs);
}
}