|
@@ -0,0 +1,135 @@
|
|
|
+package com.sf.quanrizhi.day03;
|
|
|
+
|
|
|
+import com.alibaba.druid.pool.DruidDataSource;
|
|
|
+import com.alibaba.druid.pool.DruidDataSourceFactory;
|
|
|
+import com.alibaba.druid.pool.DruidPooledConnection;
|
|
|
+import org.junit.Test;
|
|
|
+
|
|
|
+import javax.sql.DataSource;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.sql.*;
|
|
|
+import java.util.Comparator;
|
|
|
+import java.util.Properties;
|
|
|
+
|
|
|
+public class TE {
|
|
|
+
|
|
|
+
|
|
|
+ * PreparedStatement
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t1() throws ClassNotFoundException, SQLException {
|
|
|
+
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
+
|
|
|
+ Connection connection = DriverManager.getConnection("jdbc:mysql:///jdbc?characterEncoding=utf-8", "root", "root");
|
|
|
+
|
|
|
+ String sql = "insert into user(user_name,price,create_time) values (?,?,?)";
|
|
|
+ PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
|
|
+
|
|
|
+ preparedStatement.setDouble(2,123.12);
|
|
|
+ preparedStatement.setString(1,"lisa");
|
|
|
+ preparedStatement.setDate(3,new Date(System.currentTimeMillis()));
|
|
|
+ int row = preparedStatement.executeUpdate();
|
|
|
+ System.out.println(row);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 张三给李四转账500元
|
|
|
+ */
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t2(){
|
|
|
+ Connection connection = null;
|
|
|
+
|
|
|
+ try {
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
+ connection = DriverManager.getConnection("jdbc:mysql:///jdbc?characterEncoding=utf-8", "root", "root");
|
|
|
+
|
|
|
+ connection.setAutoCommit(false);
|
|
|
+ Statement statement = connection.createStatement();
|
|
|
+ String sql1 = "UPDATE account SET money = money+500 WHERE account_name='z3'";
|
|
|
+ String sql2 = "UPDATE account SET money = money-500 WHERE account_name='l4'";
|
|
|
+ statement.executeUpdate(sql1);
|
|
|
+
|
|
|
+
|
|
|
+ statement.executeUpdate(sql2);
|
|
|
+
|
|
|
+ connection.commit();
|
|
|
+ } catch (ClassNotFoundException | SQLException e) {
|
|
|
+ if (connection != null) {
|
|
|
+ try {
|
|
|
+ connection.rollback();
|
|
|
+ } catch (SQLException ex) {
|
|
|
+ throw new RuntimeException(ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t3() throws ClassNotFoundException,SQLException{
|
|
|
+
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
+ Connection connection = DriverManager.getConnection("jdbc:mysql:///jdbc?characterEncoding=utf-8", "root", "root");
|
|
|
+
|
|
|
+ connection.setAutoCommit(false);
|
|
|
+ Statement statement = connection.createStatement();
|
|
|
+ String sql1 = "UPDATE account SET money = money+500 WHERE account_name='z3'";
|
|
|
+ String sql2 = "UPDATE account SET money = money-500 WHERE account_name='l4'";
|
|
|
+ statement.addBatch(sql1);
|
|
|
+ statement.addBatch(sql2);
|
|
|
+
|
|
|
+ statement.executeBatch();
|
|
|
+ connection.commit();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void t4() throws SQLException {
|
|
|
+
|
|
|
+ DruidDataSource ds = new DruidDataSource();
|
|
|
+
|
|
|
+ ds.setUrl("jdbc:mysql:///jdbc?characterEncoding=utf-8");
|
|
|
+ ds.setUsername("root");
|
|
|
+ ds.setPassword("root");
|
|
|
+ ds.setDriverClassName("com.mysql.jdbc.Driver");
|
|
|
+
|
|
|
+ Connection connection = ds.getConnection();
|
|
|
+
|
|
|
+ String sql = "INSERT INTO user(user_name,price,create_time) VALUES(?,?,?)";
|
|
|
+ PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
|
|
+
|
|
|
+ preparedStatement.setString(1,"litiantian");
|
|
|
+ preparedStatement.setDouble(2,12.23);
|
|
|
+ preparedStatement.setDate(3,new Date(System.currentTimeMillis()));
|
|
|
+ int row = preparedStatement.executeUpdate();
|
|
|
+ System.out.println(row);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 通过获取文件信息的方式
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void t5() throws Exception {
|
|
|
+ InputStream inputStream = TE.class.getClassLoader().getResourceAsStream("db.properties");
|
|
|
+ Properties properties = new Properties();
|
|
|
+ properties.load(inputStream);
|
|
|
+
|
|
|
+ DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
|
|
|
+
|
|
|
+ Connection connection = dataSource.getConnection();
|
|
|
+ String sql = "INSERT INTO user(user_name,price,create_time) VALUES(?,?,?)";
|
|
|
+ PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
|
|
+
|
|
|
+ preparedStatement.setString(1,"llovecoding");
|
|
|
+ preparedStatement.setDouble(2,12.23);
|
|
|
+ preparedStatement.setDate(3,new Date(System.currentTimeMillis()));
|
|
|
+ int i = preparedStatement.executeUpdate();
|
|
|
+ System.out.println(i);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|