版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40348465/article/details/88016268
1.解决了mysql中日期类型的写入和读取转换问题;
2.解决了mysql中文本类型的写入和读取的问题;
3.java.util.Date和java.sql.Date的转换问题。
项目目录:
数据库表结构:
1.数据库配置文件
jdbc.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/数据库名?characterEncoding=utf-8
user=root
password=
2.编写数据库工具类
JdbcUtil.java
package utils;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JdbcUtil {
private static String url;
private static String user;
private static String password;
static {
//使用properties加载属性文件
Properties prop = new Properties();
try {
InputStream is = JdbcUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
prop.load(is);
//获取属性文件中的数据,注册驱动
String driverClassName = prop.getProperty("driverClassName");
Class.forName(driverClassName);
//获取属性文件中url、username、password
url = prop.getProperty("url");
user = prop.getProperty("user");
password = prop.getProperty("password");
}catch(Exception e) {
e.printStackTrace();
}
}
//获取数据库连接
public static Connection getConnection() {
Connection conn = null;
try {
conn =DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
//释放资源
public static void close(Connection conn,Statement stmt,ResultSet rs) {
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(stmt!=null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
3.处理日期和文件写入与读取的问题
package test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import model.Student;
import utils.JdbcUtil;
public class Test {
public static void main(String[] args) {
add();
List<Student> stuList = selectAll();
for (int i = 0; i < stuList.size(); i++) {
System.out.println(stuList.get(i));
}
}
private static boolean add() {
boolean flag = false;
Connection conn = null;
PreparedStatement stmt = null;
conn = JdbcUtil.getConnection();
String sql = "insert into student(stuName,birthday,introduce) values(?,?,?);";
try {
stmt = conn.prepareStatement(sql);
stmt.setString(1, "Tao");
// 写入日期,解决两种日期不同的问题,java.util.date->java.sql.date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date birthday = sdf.parse("2016-05-26");
System.out.println("birthday:" + birthday);
System.out.println("birthday.getTime():" + birthday.getTime());
stmt.setDate(2, new Date(birthday.getTime()));
// 写入文本
// 创建文件字符流对象
File file = new File("src\\introduce.txt");
FileReader reader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(reader);
stmt.setCharacterStream(3, bufferedReader, file.length());
int row = stmt.executeUpdate();
if (row > 0) {
System.out.println("写入成功!");
}
flag = row > 0 ? true : false;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
JdbcUtil.close(conn, stmt, null);
}
return flag;
}
private static List<Student> selectAll() {
List<Student> stuList = new ArrayList<Student>();
boolean flag = false;
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
conn = JdbcUtil.getConnection();
String sql = "select * from student";
try {
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()) {
String stuName = rs.getString("stuName");
// 获取日期,解决两种日期不同的问题,java.sql.date->java.util.date
Date birthdaySqlDate = rs.getDate("birthday");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date birthdayUtilDate = new java.util.Date(birthdaySqlDate.getTime());
// 获取文本
Reader reader = rs.getCharacterStream("introduce");
BufferedReader bufferedReader = new BufferedReader(reader);
String temp = null;
String introduce = "";
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("src\\introduce2.txt"));
while ((temp = bufferedReader.readLine()) != null) {
bufferedWriter.write(temp);
introduce += temp;
}
bufferedWriter.close();
bufferedReader.close();
Student student = new Student(stuName, birthdayUtilDate, introduce);
stuList.add(student);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
JdbcUtil.close(conn, stmt, rs);
}
return stuList;
}
}