标签:rac 程序 demo 编写 创建 ati array value .data
@Test public void test() throws Exception { //加载驱动 Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "123456"; //获取连接 Connection connection = DriverManager.getConnection(url, user, password); String sql = " insert into user(name) values(?)"; PreparedStatement pstmt = connection.prepareStatement(sql); for (int i = 0; i < 10; i++) { pstmt.setString(1,"许威威"+i); pstmt.addBatch(); } pstmt.executeBatch(); pstmt.close(); connection.close(); }
@Test public void test() throws Exception { //加载驱动 Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "123456"; //获取连接 Connection connection = DriverManager.getConnection(url, user, password); String sql = " insert into user(name) values(?)"; PreparedStatement pstmt = connection.prepareStatement(sql); for (int i = 0; i < 10000; i++) { pstmt.setString(1,"许威威"+i); if(i % 1000 ==0){ pstmt.addBatch(); pstmt.clearBatch(); } } pstmt.executeBatch(); pstmt.close(); connection.close(); }
delimiter $ create procedure proc(IN num int ,out name VARCHAR(20)) BEGIN SELECT `user`.`name` into name from user where `user`.id = num ; end $
package com.xuweiwei; import org.junit.Test; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Types; public class jdbc2 { @Test public void test() throws Exception { //加载驱动 Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "123456"; //获取连接 Connection connection = DriverManager.getConnection(url, user, password); CallableStatement callableStatement = connection.prepareCall(" { call proc(?,?)} "); callableStatement.setInt(1,1); callableStatement.registerOutParameter(2, Types.VARBINARY); callableStatement.execute(); String string = callableStatement.getString(2); System.out.println(string); connection.close(); } }
package com.xuweiwei.pool; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class ConnectionPoolDemo { //内置一个连接池 private static List<Connection> pool = new ArrayList<>(); static { for (int i = 0; i < 10; i++) { //加载驱动 try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "123456"; //获取连接 try { Connection conn = DriverManager.getConnection(url, user, password); pool.add(conn); } catch (SQLException e) { e.printStackTrace(); } } } /** * 从连接池中获取一个连接 * * @return */ public synchronized static Connection getConnection() { if (pool.size() > 0) { return pool.remove(0); } else { throw new RuntimeException("服务器真忙"); } } /** * 将连接还到连接池里 * * @param connection */ public synchronized static void release(Connection connection) { pool.add(connection); } }
package com.xuweiwei.pool; import javax.sql.DataSource; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.logging.Logger; public class MyDataSource1 implements DataSource { //内置一个连接池 private static List<Connection> pool = Collections.synchronizedList(new ArrayList<>()); static { for (int i = 0; i < 20; i++) { //加载驱动 try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "123456"; //获取连接 try { Connection conn = DriverManager.getConnection(url, user, password); pool.add(conn); } catch (SQLException e) { e.printStackTrace(); } } } /** * 从池子里获取连接 * @return * @throws SQLException */ @Override public Connection getConnection() throws SQLException { if(null != pool && pool.size() > 0){ return pool.remove(0); }else{ throw new RuntimeException("服务器正忙"); } } @Override public Connection getConnection(String username, String password) throws SQLException { return null; } @Override public <T> T unwrap(Class<T> iface) throws SQLException { return null; } @Override public boolean isWrapperFor(Class<?> iface) throws SQLException { return false; } @Override public PrintWriter getLogWriter() throws SQLException { return null; } @Override public void setLogWriter(PrintWriter out) throws SQLException { } @Override public void setLoginTimeout(int seconds) throws SQLException { } @Override public int getLoginTimeout() throws SQLException { return 0; } @Override public Logger getParentLogger() throws SQLFeatureNotSupportedException { return null; } }
标签:rac 程序 demo 编写 创建 ati array value .data
原文地址:https://www.cnblogs.com/xuweiweiwoaini/p/9656920.html