Service.java
// 添加测试
UserDao userDao = DaoFactory.getUserDao();
User user = new User();
user.setUsername("小明");
user.setAge(23);
userDao.addUser(user);
// 查询测试
User user1 = userDao.getUserByUsername("小明");
System.out.println(user1);
12.util包下的Date与sql包下的时间类型之间的转换
Data -> java.sql.Data
Time -> java.sql.Time
Timestamp -> java.sql.Timestamp
领域对象中所有属性不能出现java.sql包内容
继承关系
java.util.Date
-java.sql.Date
父类转子类:util.Data -> sql.Date、Time、Timestamp
java.util.Date UtilDate = new java.util.Date(); long longDate = UtilDate.getTime(); java.sql.Date sqlData = new java.sql.Date(longDate);
子类转父类:sql.Date、Time、Timestamp -> util.Data
java.util.Date UtilDate = new java.sql.Date(System.currentTimeMillis());
13.大数据
可以将文件存入MySQL
my.ini配置
max_allowed_packet=10485760
14.批处理
批处理只针对更新(增,删,改)
一次向服务器发送多条sql语句
开启批处理参数
rewriteBatchedStatements=true
dbconfig.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/data?rewriteBatchedStatements=true
username=root
password=123456
import util.JdbcUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
class Demo {
public static void main(String[] args) throws SQLException {
Connection conn = JdbcUtils.getConnection();
String sql = "insert into user(username)values(?)";
PreparedStatement statement = conn.prepareStatement(sql);
for(int i=0; i<10000; i++){
statement.setString(1, "name" + i);
statement.addBatch(); // 装箱
}
long start = System.currentTimeMillis();
statement.executeBatch(); // 提交数据
long end = System.currentTimeMillis();
System.out.println(end - start); // 107
}
}
结束语
本文主要讲了java数据库开发中JDBC基础使用方法及实例
更多关于java数据库开发之JDBC基础使用方法及实例请查看下面的相关链接










