数据库表格的插入信息

在完成大部分业务逻辑后,我们需要将信息存入数据库中

我们使用mysql数据库,使用jdbc连接数据库,

需要下载:

mysql-connector-java-5.1.39-bin.jar,

将jar放入项目中,右击build path,

这样就可以将mysql-connector添加到path中

以下是我的数据库插入信息的代码:


package myThread;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.List;

import org.json.JSONObject;

import com.mysql.jdbc.Connection;
//import com.mysql.jdbc.Statement;

import request.stamp_struct;
import request.stamp_struct2;

public class Database {
	// 数据库连接字符串,这里的shixun为数据库名
	private static final String URL = "jdbc:mysql://localhost:3306/shixun?";
	private static final String NAME = "root";// 登录名
	private static final String PASSWORD = "132456";// 密码

	public Connection TheSqlConnection() {
		// 1.加载驱动
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			System.out.println("未能成功加载驱动程序,请检查是否导入驱动程序!");
			// 添加一个println,如果加载驱动异常,检查是否添加驱动,或者添加驱动字符串是否错误
			e.printStackTrace();
		}
		Connection conn = null;
		try {
			conn = (Connection) DriverManager.getConnection(URL, NAME, PASSWORD);
			System.out.println("获取数据库连接成功!");
		} catch (SQLException e) {
			System.out.println("获取数据库连接失败!");
			// 添加一个println,如果连接失败,检查连接字符串或者登录名以及密码是否错误
			e.printStackTrace();
		}
		// 数据库打开后就要关闭
		if (conn != null) {
			return conn;
		} else
			return null;
	}
	public boolean insert_emotion(Connection conn,List list) {
		JSONObject jsonobj;
		Statement st = null;
		try {
			st = conn.createStatement();
		} catch (Exception e) {
			e.getMessage();
		}
		System.out.println("*********insert_emotion*********");
		for (int n = 0; n < list.size(); n++) {
			jsonobj = list.get(n).jsonobject;
			String time = list.get(n).date;
			String classroom_id = list.get(n).classroom_id;
			//老师的id
			String teacher_id = "1236";
			String face_id=jsonobj.getString("face_token");
			//获取属性
			JSONObject attributes=jsonobj.getJSONObject("attributes");
			//获取情绪
			JSONObject emotion=attributes.getJSONObject("emotion");
			//获取左眼
			JSONObject l_eyegaze=attributes.getJSONObject("eyegaze").getJSONObject("left_eye_gaze");
			//获取右眼
			JSONObject r_eyegaze=attributes.getJSONObject("eyegaze").getJSONObject("right_eye_gaze");
			//以下是7中情绪
			double happiness= emotion.getDouble("happiness");
			double surprise=emotion.getDouble("surprise");
			double neutral= emotion.getDouble("neutral");
			double sadness= emotion.getDouble("sadness");
			double disgust= emotion.getDouble("disgust");
			double anger= emotion.getDouble("anger");
			double fear=emotion.getDouble("fear");
			//以下是左眼的视线
			double l_x = l_eyegaze.getDouble("vector_x_component");
			double l_y = l_eyegaze.getDouble("vector_y_component");
			double l_z = l_eyegaze.getDouble("vector_z_component");
			//以下是右眼的视线
			double r_x = r_eyegaze.getDouble("vector_x_component");
			double r_y = r_eyegaze.getDouble("vector_y_component");
			double r_z = r_eyegaze.getDouble("vector_z_component");
			//System.out.println("####################inserting...");
			insert_con(st,time,classroom_id,teacher_id,face_id,l_x,l_y,l_z,r_x,r_y,r_z);
			insert_emotion2(st,time,classroom_id,teacher_id,face_id,happiness,fear,surprise,anger,disgust,neutral,sadness);
		}
		System.out.println("return ...");
		return false;
	}
	public boolean insert_word(Connection con,List list)
	{
		try {
			Statement st=con.createStatement();
			System.out.println("******************************try inserting...");
			for(int i=0;i

你可能感兴趣的:(数据库表格的插入信息)