2024/5/4(冲刺第八天)
所学时间:2小时
代码行数:100
博客园数:1篇
所学知识:加班第三天
package com.example.kanglaojian;
import android.util.Log;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class MySQLConnector {
private static final String TAG = "MySQLConnector";
public static Connection getConn() throws SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
Log.v(TAG, "加载JDBC驱动成功");
} catch (ClassNotFoundException e) {
Log.e(TAG, "加载JDBC驱动失败");
}
// 2.设置好IP/端口/数据库名/用户名/密码等必要的连接信息
String ip = "172.18.44.61";
int port = 3306;
String dbName = "kanglaojian";
String url = "jdbc:mysql://" + ip + ":" + port + "/" + dbName + "?useUnicode=true&characterEncoding=utf-8&useSSL=false";
// 构建连接mysql的字符串
String user = "root";
String Password = "123456";
// 3.连接JDBC
java.sql.Connection connection= DriverManager.getConnection(url, user, Password);
return connection;
}
public static void insertOlder(String name,String sex,String phone,String id,String strpassword) {
final Thread thread = new Thread(new Runnable() {
public void run() {
try {
Connection connection=getConn();
Statement statement=connection.createStatement();
String sql = "INSERT INTO older(name,sex,phonenumber,idcard,passwords) VALUES(?,?,?,?,?)";
Log.e("1111111111111111111111","INSERT INTO older(name,sex,phonenumber,idcard,passwords) VALUES(?,?,?,?,?)");
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1,name);
ps.setString(2,sex);
ps.setString(3,phone);
ps.setString(4,id);
ps.setString(5,strpassword);
ps.executeUpdate();
Log.d(TAG, "数据库连接成功");
connection.close();
return;
} catch (SQLException e) {
Log.e("13","3223");
throw new RuntimeException(e);
}
}
});
thread.start();
}
public static void insertOlder1(String birthday,String address){
final Thread thread = new Thread(new Runnable() {
public void run() {
try {
Connection connection=getConn();
Statement statement=connection.createStatement();
String sql = "INSERT INTO older(birthdate,address) VALUES(?,?)";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1,birthday);
ps.setString(2,address);
ps.executeUpdate();
Log.d(TAG, "数据库连接成功");
connection.close();
return;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
});
thread.start();
}
public static void insert_check_in(String str_blood_pressure,String str_blood_suger,String str_blood_oxygen,String str_blood_lipids,String str_heart_rate,String blood_pressure_case,String blood_suger_case,String blood_oxygen_case,String blood_lipids_case,String heart_rate_case,String healthstatus){
final Thread thread = new Thread(new Runnable() {
public void run() {
try {
Connection connection=getConn();
Statement statement=connection.createStatement();
String sql = "INSERT INTO clock(bloodpressure,bloodpressurecase,bloodsugar,bloodsugarcase,bloodoxygen,bloodoxygencase,bloodlipids,bloodlipidscase,heartrate,heartratecase, healthstatus) VALUES(?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1,str_blood_pressure);
ps.setString(2,blood_pressure_case);
ps.setString(3,str_blood_suger);
ps.setString(4,blood_suger_case);
ps.setString(5,str_blood_oxygen);
ps.setString(6,blood_oxygen_case);
ps.setString(7,str_blood_lipids);
ps.setString(8,blood_lipids_case);
ps.setString(9,str_heart_rate);
ps.setString(10,heart_rate_case);
ps.setString(11,healthstatus);
ps.executeUpdate();
Log.d(TAG, "数据库连接成功");
connection.close();
return;
}catch (SQLException e) {
throw new RuntimeException(e);
}
}
});
thread.start();
}
}
import android.util.Log;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class MySQLConnector {
private static final String TAG = "MySQLConnector";
public static Connection getConn() throws SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
Log.v(TAG, "加载JDBC驱动成功");
} catch (ClassNotFoundException e) {
Log.e(TAG, "加载JDBC驱动失败");
}
// 2.设置好IP/端口/数据库名/用户名/密码等必要的连接信息
String ip = "172.18.44.61";
int port = 3306;
String dbName = "kanglaojian";
String url = "jdbc:mysql://" + ip + ":" + port + "/" + dbName + "?useUnicode=true&characterEncoding=utf-8&useSSL=false";
// 构建连接mysql的字符串
String user = "root";
String Password = "123456";
// 3.连接JDBC
java.sql.Connection connection= DriverManager.getConnection(url, user, Password);
return connection;
}
public static void insertOlder(String name,String sex,String phone,String id,String strpassword) {
final Thread thread = new Thread(new Runnable() {
public void run() {
try {
Connection connection=getConn();
Statement statement=connection.createStatement();
String sql = "INSERT INTO older(name,sex,phonenumber,idcard,passwords) VALUES(?,?,?,?,?)";
Log.e("1111111111111111111111","INSERT INTO older(name,sex,phonenumber,idcard,passwords) VALUES(?,?,?,?,?)");
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1,name);
ps.setString(2,sex);
ps.setString(3,phone);
ps.setString(4,id);
ps.setString(5,strpassword);
ps.executeUpdate();
Log.d(TAG, "数据库连接成功");
connection.close();
return;
} catch (SQLException e) {
Log.e("13","3223");
throw new RuntimeException(e);
}
}
});
thread.start();
}
public static void insertOlder1(String birthday,String address){
final Thread thread = new Thread(new Runnable() {
public void run() {
try {
Connection connection=getConn();
Statement statement=connection.createStatement();
String sql = "INSERT INTO older(birthdate,address) VALUES(?,?)";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1,birthday);
ps.setString(2,address);
ps.executeUpdate();
Log.d(TAG, "数据库连接成功");
connection.close();
return;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
});
thread.start();
}
public static void insert_check_in(String str_blood_pressure,String str_blood_suger,String str_blood_oxygen,String str_blood_lipids,String str_heart_rate,String blood_pressure_case,String blood_suger_case,String blood_oxygen_case,String blood_lipids_case,String heart_rate_case,String healthstatus){
final Thread thread = new Thread(new Runnable() {
public void run() {
try {
Connection connection=getConn();
Statement statement=connection.createStatement();
String sql = "INSERT INTO clock(bloodpressure,bloodpressurecase,bloodsugar,bloodsugarcase,bloodoxygen,bloodoxygencase,bloodlipids,bloodlipidscase,heartrate,heartratecase, healthstatus) VALUES(?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1,str_blood_pressure);
ps.setString(2,blood_pressure_case);
ps.setString(3,str_blood_suger);
ps.setString(4,blood_suger_case);
ps.setString(5,str_blood_oxygen);
ps.setString(6,blood_oxygen_case);
ps.setString(7,str_blood_lipids);
ps.setString(8,blood_lipids_case);
ps.setString(9,str_heart_rate);
ps.setString(10,heart_rate_case);
ps.setString(11,healthstatus);
ps.executeUpdate();
Log.d(TAG, "数据库连接成功");
connection.close();
return;
}catch (SQLException e) {
throw new RuntimeException(e);
}
}
});
thread.start();
}
}