现在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));
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。然而其他语言没有依赖于这样的特性。
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…)
不改动源码的情况下,对原有功能进行扩展或增强 装饰者模式和代理模式可以实现,spring框架AOP使用动态代理。因为装饰者模式和静态代理都需要写扩展类
AspectJ实际上是对AOP编程思想的一个实践,当然,除了AspectJ以外,还有很多其它的AOP实现,例如ASMDex,但目前最好、最方便的,依然是AspectJ
