JDBCUtils 數據庫操作工具 及 mysql 時間插入問題

本次是數據庫重構,數據進行遷移時mysql數據關於時間的問題解決。異常爲

com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '1989' for column 'check_time' at row 1
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4230)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4164)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2615)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2776)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2838)
	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)
	at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1307)
	at com.jeeplus.common.utils.jdbc.JDBCUtils.main(JDBCUtils.java:126)

sql 語句如下

insert into biz_topic_result (id,patient_id,sample_id,title,check_time,code,photo_url,result_analyze,type,create_by,create_date,update_by,update_date,remarks,del_flag)values('5c2ef8545b724ce5955f3ca5dfc5dd9f','6d98b4b4188e40eda1482ac185071011','aecb6447b9084f6eae509ab8d1728bcb','測試',2018-08-21,'201808211624',null,'正常','topic_result_type_1',1,'2018-08-21',1,'2018-08-21','null',0);

 時間類型出錯。查閱百度後使用日期格式化工具處理

SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
user.setCreateTime(Timestamp.valueOf(simpleDate.format(nowdate)));

sql 語句如下

insert into biz_topic_result (id,patient_id,sample_id,title,check_time,code,photo_url,result_analyze,type,create_by,create_date,update_by,update_date,remarks,del_flag)values('5c2ef8545b724ce5955f3ca5dfc5dd9f','6d98b4b4188e40eda1482ac185071011','aecb6447b9084f6eae509ab8d1728bcb','測試',2018-08-21 00:00:00.0,'201808211624',null,'正常','topic_result_type_1',1,'2018-08-21 00:00:00.0',1,'2018-08-21 00:00:00.0','null',0);

刨錯異常如下

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '00:00:00.0,'201808211624',null,'正常','topic_result_type_1',1,'2018-08-21',1,'' at line 1
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
	at com.mysql.jdbc.Util.getInstance(Util.java:384)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4232)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4164)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2615)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2776)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2838)
	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)
	at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1307)
	at com.jeeplus.common.utils.jdbc.JDBCUtils.main(JDBCUtils.java:128)

後來將其修改爲日期格式  2018-08-21,工具類操作不了數據庫。使用Navicat 可以插入數據,但是時間值全部爲 0000:00:00  00:00。也就是完全插入的空值。

最後通過使用 SimpleDateFormat格式化爲String類型。插入成功。

下面爲此次JDBCUtils工具以及使用

package com.jeeplus.common.utils.jdbc;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

import com.jeeplus.common.utils.IdGen;
import com.jeeplus.common.utils.PropertiesLoader;


public class JDBCUtils {
	//鏈接數據庫
	public Connection getCon(){
		Connection conn = null;
		PropertiesLoader loader = new PropertiesLoader("xsd.properties");
		String driver = loader.getProperty("jdbc.driver");
		String url = "jdbc:mysql://localhost:3306/hematopathy_demo?useUnicode=true&characterEncoding=UTF-8";
		String userName = loader.getProperty("jdbc.username");
		String password = loader.getProperty("jdbc.password");
		//System.out.println(url);
		try {
			Class.forName(driver);
			conn=DriverManager.getConnection(url, userName, password);

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	//關閉數據庫
	public void closeConn(){
		Connection con = getCon();
		try {
			if(!con.isClosed()){
				con.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public Connection getConjeple(){
		Connection conn = null;
		PropertiesLoader loader = new PropertiesLoader("slave.properties");
		String driver = loader.getProperty("jdbc.driver");
		String url = loader.getProperty("jdbc.url");
		String userName = loader.getProperty("jdbc.username");
		String password = loader.getProperty("jdbc.password");
		//System.out.println(url);
		try {
			Class.forName(driver);
			conn=DriverManager.getConnection(url, userName, password);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	//關閉數據庫
	public void closeConnjeple(){
		Connection con = getConjeple();
		try {
			if(!con.isClosed()){
				con.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd"); 

		try {
			JDBCUtils utils = new JDBCUtils();
			Connection con = utils.getCon();
			String sql="select * from biz_check_result";
			PreparedStatement statement = con.prepareStatement(sql);
			ResultSet resultSet = statement.executeQuery();
			int i=0;
			Connection conjeple = utils.getConjeple();
			while(resultSet.next()){
				
				i++;
				int iid = resultSet.getInt(1);
				String id = "'"+resultSet.getString(2)+"'";
				String patient ="'"+ resultSet.getString(3)+"'";
				String sample = "'"+resultSet.getString(4)+"'";
				String item ="'"+ resultSet.getString(5)+"'";
				String Pitem ="'"+ resultSet.getString(6)+"'";
				String code ="'"+ resultSet.getString(7)+"'";
				String checkTime ="'"+sdf.format(resultSet.getDate(8))+"'";
				String abnormal ="'"+ resultSet.getString(9)+"'";
				String result = "'"+resultSet.getString(10)+"'";
				String resultAnalyse ="'"+ resultSet.getString(11)+"'";
				String title ="'"+ resultSet.getString(12)+"'";
				String photourls = resultSet.getString(13);
				int is_checked = resultSet.getInt(17);
				String remarks ="'"+ resultSet.getString(18)+"'";
				String creatDate ="'"+sdf.format(resultSet.getDate(19))+"'" ;
				String creatBy = resultSet.getString(20);
				String updatey = resultSet.getString(21);
				String updateDate = "'"+sdf.format(resultSet.getDate(22))+"'";
				String delflag = resultSet.getString(23);
				//分子學檢測結果主體
				String type="'"+"topic_result_type_1"+"'";
				String sqlcheckResult="insert into biz_topic_result (id,patient_id,sample_id,title,check_time,code,photo_url,result_analyze,type,create_by,create_date,update_by,update_date,remarks,del_flag)"
						+"values("+id+","+patient+","+sample+","+title+","+checkTime+","+code+",null,"+resultAnalyse+","+type+","+creatBy+","+creatDate+","+updatey+","+updateDate+","+remarks+","+delflag+");";
				//System.out.println(sqlcheckResult);
				PreparedStatement prepareStatement = conjeple.prepareStatement(sqlcheckResult);
				
				prepareStatement.execute();// 提交執行sql語句
				String uuid ="'"+ IdGen.uuid()+"'";
				String sqlResult="insert into biz_check_result (id,patient_id,topresult_id,item_id,p_item,abnormal,result,is_checked,has_site_locus,remarks,create_date,create_by,update_by,update_date,del_flag)"+
				"values("+uuid+","+patient+","+id+","+item+","+Pitem+","+abnormal+","+result+","+is_checked+",null,"+remarks+","+creatDate+","+creatBy+","+updatey+","+updateDate+","+delflag+")";
				System.out.println(sqlResult);
				PreparedStatement resultInsert = conjeple.prepareStatement(sqlResult);
				resultInsert.execute();
				}
			System.out.println(i);
			utils.closeConn();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章