java

it2025-11-13  15

1. java Date中方法toLocaleString过时的替代方案

现在toLocaleString()方法已过时,由DateFormat.format(Date date)取代。 DateFormat ddf = DateFormat.getDateInstance(); DateFormat dtf = DateFormat.getTimeInstance(); DateFormat ddtf = DateFormat.getDateTimeInstance(); Date date = new Date(); System.out.println(“日期:” + ddf.format(date)); System.out.println(“时间:” + dtf.format(date)); System.out.println(“日期时间:” + ddtf.format(date)); SimpleDateFormat sdf = (SimpleDateFormat) DateFormat.getDateTimeInstance(); System.out.println(“日期时间:” + sdf.format(date));

2.mysql 连接

utf8mb4比utf8多了emoji编码支持. 使用了Mysql Connector/J 6.x以上的版本,在配置url的时候不能简单写成 : jdbc:mysql://localhost:3306/yzu 而是要写成 : jdbc:mysql://localhost:3306/yzu?serverTimezone=UTC 北京时间使用 Asia/Shanghai

5.x版本的驱动文件jar包对应的是: Class.forName(“com.mysql.jdbc.Driver”); 语句来加载数据库驱动

8.0x版本的驱动文件jar包对应的是: Class.forName(“com.mysql.cj.jdbc.Driver”);

mysql8.x使用url连接,写utf8mb4会报错,原因如下: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8

Java语言里面所实现的UTF-8编码就是支持4字节的,所以不需要配置 mb4 这样的字眼,但如果从MySQL读写emoji,MySQL驱动版本要在 5.1.13 及以上版本,数据库连接依然是 characterEncoding=UTF-8 。

但还没完,遇到一个大坑。官方手册 里还有这么一段话:

Connector/J did not support utf8mb4 for servers 5.5.2 and newer.

Connector/J now auto-detects servers configured with character_set_server=utf8mb4 or treats the Java encoding utf-8 passed using characterEncoding=… as utf8mb4 in the SET NAMES= calls it makes when establishing the connection. (Bug #54175)

意思是,java驱动会自动检测服务端 character_set_server 的配置,如果为utf8mb4,驱动在建立连接的时候设置 SET NAMES utf8mb4。然而其他语言没有依赖于这样的特性。

3.JDBC

statement会有sql注入问题,使用PreparedStatement MySQL驱动版本不同,驱动名有差异

//使用jdbc.properties配置文件 Class.forName("com.mysql.cj.jdbc.Driver"); Properties properties = new Properties(); InputStream resourceAsStream = JDBCutils.class.getClassLoader().getResourceAsStream("jdbc.properties"); properties.load(resourceAsStream); String url = properties.getProperty("url"); String user = properties.getProperty("user"); String password = properties.getProperty("password"); Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8", "root", "mysql3306"); String sql = "select * from user_info where username=? and password=?"; PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1,"紫烟‘ --"); preparedStatement.setString(2,"asd123"); ResultSet resultSet = preparedStatement.executeQuery(); if(resultSet.next()){ System.out.println("登录成功"); }else{ System.out.println("登录失败"); } //释放资源 if (statement != null) { try { statement.close(); } catch ( SQLException sqlex) { statement = null; } } if (connection != null) { try { connection.close(); } catch (SQLException sqlex) { connection = null; } } if (resultSet != null) { try { resultSet.close(); } catch (SQLException sqlex) { resultSet = null; } }

//c3p0使用

@Test public void testPool() throws PropertyVetoException, SQLException { ComboPooledDataSource cpds = new ComboPooledDataSource(); cpds.setDriverClass( "com.mysql.cj.jdbc.Driver" ); //loads the jdbc driver cpds.setJdbcUrl( "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8" ); cpds.setUser("root"); cpds.setPassword("mysql3306"); Connection connection = cpds.getConnection(); System.out.println(connection); connection.close(); }

C3P0: c3p0-config.xml 名称必须是固定,必须放在resources下面。编码写在配置文件第一行 new ComboPooledDataSource(); 通常一个程序只会创建一个连接池 ```

<c3p0-config encoding="UTF-8"> <default-config> <property name="driverClass">com.mysql.cj.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/test?serverTimezone=UTC</property> <property name="user">root</property> <property name="password">mysql3306</property> <property name="automaticTestTable">con_test</property> <property name="checkoutTimeout">30000</property> <property name="idleConnectionTestPeriod">30</property> <property name="initialPoolSize">10</property> <property name="maxIdleTime">30</property> <property name="maxPoolSize">100</property> <property name="minPoolSize">10</property> <property name="maxStatements">200</property> </default-config> </c3p0-config>

commons-dbutils,通常使用的jar包 QueryRunner(池子) update(sql,params…)

4.JDBCutils.java

package com.demo.util; import com.mchange.v2.c3p0.ComboPooledDataSource; import javax.sql.DataSource; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * @Description: 抽取工具类 * * 1. 注册驱动 * * 2.对外提供:获取连接的方法 * * 3.对外提供: 释放资源的方法 */ public class JDBCUtils { //一个程序中,通常只会有一个池子 private static ComboPooledDataSource dataSource = new ComboPooledDataSource(); //返回连接池 public static DataSource getDataSource(){ return dataSource; } /** * @return 获取连接对象的方法 * @throws Exception */ public static Connection getConnection() throws Exception { //从连接池中去获取 return dataSource.getConnection(); } /** * 释放资源 */ public static void release(Connection connection,Statement statement){ release(connection,statement,null); } /** * 释放资源的方法 */ public static void release(Connection connection, Statement statement, ResultSet resultSet) { if (resultSet != null) { try { resultSet.close(); } catch (SQLException sqlEx) { // ignore } resultSet = null; //GC 垃圾回收器, 让回收器尽快回收 } } if (statement != null) { try { statement.close(); } catch (SQLException sqlEx) { // ignore } statement = null; } } if (connection != null) { try { connection.close(); } catch (SQLException sqlEx) { // ignore } connection = null; } } } }

5.AOP

不改动源码的情况下,对原有功能进行扩展或增强 装饰者模式和代理模式可以实现,spring框架AOP使用动态代理。因为装饰者模式和静态代理都需要写扩展类

1. 动态代理实现方式

JDKCglib 如果真实类是一个普通类,没有实现接口,那么就采用这种方式, 创建出来真实类的子类作为代理类。

2.AspectJ

AspectJ实际上是对AOP编程思想的一个实践,当然,除了AspectJ以外,还有很多其它的AOP实现,例如ASMDex,但目前最好、最方便的,依然是AspectJ

最新回复(0)