最近学习了JDBC的相关知识,写一下自定义连接池
一些说明:
本代码参考了部分别人的代码!!!
JDBCCon类具体创建了连接;
MyConnection类集成了Connection类用来管理连接与池,其中的close方法必须pool.add();
MyDataSource则是具体实现连接池。
具体步骤:
1、导mysql的jar包
2、配置db.propertites
1 driver = com.mysql.cj.jdbc.Driver2 url = jdbc:mysql://localhost:3306/test?serverTimezone=GMT3 username = root4 password = root
3、JDBCCon
1 package com.nick.util; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.sql.Connection; 6 import java.sql.DriverManager; 7 import java.sql.PreparedStatement; 8 import java.sql.ResultSet; 9 import java.sql.SQLException;10 import java.util.Properties;11 12 public class JDBCCon {13 14 private static String driver; 15 private static String url;16 private static String username;17 private static String password;18 19 20 static {21 22 try {23 ClassLoader classLoader = JDBCCon.class.getClassLoader();24 InputStream is = classLoader.getResourceAsStream("db.properties");25 Properties props = new Properties();26 props.load(is);27 driver = props.getProperty("driver");28 url = props.getProperty("url");29 username = props.getProperty("username");30 password = props.getProperty("password");31 } catch (IOException e) {32 // TODO Auto-generated catch block33 e.printStackTrace();34 }35 36 }37 38 public static Connection getConnection() {39 Connection connection = null;40 try {41 Class.forName(driver);42 connection = DriverManager.getConnection(url, username, password);43 } catch (Exception e) {44 // TODO: handle exception45 }46 return connection;47 48 }49 50 public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) {51 if(rs != null) {52 try {53 rs.close();54 } catch (SQLException e) {55 // TODO Auto-generated catch block56 e.printStackTrace();57 }58 }59 if(pstmt != null) {60 try {61 pstmt.close();62 } catch (SQLException e) {63 // TODO Auto-generated catch block64 e.printStackTrace();65 }66 }67 if(conn != null) {68 try {69 conn.close();70 } catch (SQLException e) {71 // TODO Auto-generated catch block72 e.printStackTrace();73 }74 }75 76 }77 78 }
4、MyConnection
1 package com.nick.dataSource; 2 3 import java.sql.Array; 4 import java.sql.Blob; 5 import java.sql.CallableStatement; 6 import java.sql.Clob; 7 import java.sql.Connection; 8 import java.sql.DatabaseMetaData; 9 import java.sql.NClob; 10 import java.sql.PreparedStatement; 11 import java.sql.SQLClientInfoException; 12 import java.sql.SQLException; 13 import java.sql.SQLWarning; 14 import java.sql.SQLXML; 15 import java.sql.Savepoint; 16 import java.sql.Statement; 17 import java.sql.Struct; 18 import java.util.LinkedList; 19 import java.util.Map; 20 import java.util.Properties; 21 import java.util.concurrent.Executor; 22 23 public class MyConnection implements Connection{ 24 25 private Connection connection = null; 26 private LinkedListpool = null; 27 public MyConnection(Connection connection, LinkedList pool) { 28 this.connection = connection; 29 this.pool = pool; 30 } 31 32 @Override 33 public void close() throws SQLException { 34 // TODO Auto-generated method stub 35 pool.add(connection); 36 } 37 38 @Override 39 public T unwrap(Class iface) throws SQLException { 40 // TODO Auto-generated method stub 41 return null; 42 } 43 @Override 44 public boolean isWrapperFor(Class iface) throws SQLException { 45 // TODO Auto-generated method stub 46 return false; 47 } 48 @Override 49 public Statement createStatement() throws SQLException { 50 // TODO Auto-generated method stub 51 return null; 52 } 53 @Override 54 public PreparedStatement prepareStatement(String sql) throws SQLException { 55 // TODO Auto-generated method stub 56 return null; 57 } 58 @Override 59 public CallableStatement prepareCall(String sql) throws SQLException { 60 // TODO Auto-generated method stub 61 return null; 62 } 63 @Override 64 public String nativeSQL(String sql) throws SQLException { 65 // TODO Auto-generated method stub 66 return null; 67 } 68 @Override 69 public void setAutoCommit(boolean autoCommit) throws SQLException { 70 // TODO Auto-generated method stub 71 72 } 73 @Override 74 public boolean getAutoCommit() throws SQLException { 75 // TODO Auto-generated method stub 76 return false; 77 } 78 @Override 79 public void commit() throws SQLException { 80 // TODO Auto-generated method stub 81 82 } 83 @Override 84 public void rollback() throws SQLException { 85 // TODO Auto-generated method stub 86 87 } 88 89 @Override 90 public boolean isClosed() throws SQLException { 91 // TODO Auto-generated method stub 92 return false; 93 } 94 @Override 95 public DatabaseMetaData getMetaData() throws SQLException { 96 // TODO Auto-generated method stub 97 return null; 98 } 99 @Override100 public void setReadOnly(boolean readOnly) throws SQLException {101 // TODO Auto-generated method stub102 103 }104 @Override105 public boolean isReadOnly() throws SQLException {106 // TODO Auto-generated method stub107 return false;108 }109 @Override110 public void setCatalog(String catalog) throws SQLException {111 // TODO Auto-generated method stub112 113 }114 @Override115 public String getCatalog() throws SQLException {116 // TODO Auto-generated method stub117 return null;118 }119 @Override120 public void setTransactionIsolation(int level) throws SQLException {121 // TODO Auto-generated method stub122 123 }124 @Override125 public int getTransactionIsolation() throws SQLException {126 // TODO Auto-generated method stub127 return 0;128 }129 @Override130 public SQLWarning getWarnings() throws SQLException {131 // TODO Auto-generated method stub132 return null;133 }134 @Override135 public void clearWarnings() throws SQLException {136 // TODO Auto-generated method stub137 138 }139 @Override140 public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {141 // TODO Auto-generated method stub142 return null;143 }144 @Override145 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)146 throws SQLException {147 // TODO Auto-generated method stub148 return null;149 }150 @Override151 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {152 // TODO Auto-generated method stub153 return null;154 }155 @Override156 public Map > getTypeMap() throws SQLException {157 // TODO Auto-generated method stub158 return null;159 }160 @Override161 public void setTypeMap(Map > map) throws SQLException {162 // TODO Auto-generated method stub163 164 }165 @Override166 public void setHoldability(int holdability) throws SQLException {167 // TODO Auto-generated method stub168 169 }170 @Override171 public int getHoldability() throws SQLException {172 // TODO Auto-generated method stub173 return 0;174 }175 @Override176 public Savepoint setSavepoint() throws SQLException {177 // TODO Auto-generated method stub178 return null;179 }180 @Override181 public Savepoint setSavepoint(String name) throws SQLException {182 // TODO Auto-generated method stub183 return null;184 }185 @Override186 public void rollback(Savepoint savepoint) throws SQLException {187 // TODO Auto-generated method stub188 189 }190 @Override191 public void releaseSavepoint(Savepoint savepoint) throws SQLException {192 // TODO Auto-generated method stub193 194 }195 @Override196 public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)197 throws SQLException {198 // TODO Auto-generated method stub199 return null;200 }201 @Override202 public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency,203 int resultSetHoldability) throws SQLException {204 // TODO Auto-generated method stub205 return null;206 }207 @Override208 public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency,209 int resultSetHoldability) throws SQLException {210 // TODO Auto-generated method stub211 return null;212 }213 @Override214 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {215 // TODO Auto-generated method stub216 return null;217 }218 @Override219 public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException {220 // TODO Auto-generated method stub221 return null;222 }223 @Override224 public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {225 // TODO Auto-generated method stub226 return null;227 }228 @Override229 public Clob createClob() throws SQLException {230 // TODO Auto-generated method stub231 return null;232 }233 @Override234 public Blob createBlob() throws SQLException {235 // TODO Auto-generated method stub236 return null;237 }238 @Override239 public NClob createNClob() throws SQLException {240 // TODO Auto-generated method stub241 return null;242 }243 @Override244 public SQLXML createSQLXML() throws SQLException {245 // TODO Auto-generated method stub246 return null;247 }248 @Override249 public boolean isValid(int timeout) throws SQLException {250 // TODO Auto-generated method stub251 return false;252 }253 @Override254 public void setClientInfo(String name, String value) throws SQLClientInfoException {255 // TODO Auto-generated method stub256 257 }258 @Override259 public void setClientInfo(Properties properties) throws SQLClientInfoException {260 // TODO Auto-generated method stub261 262 }263 @Override264 public String getClientInfo(String name) throws SQLException {265 // TODO Auto-generated method stub266 return null;267 }268 @Override269 public Properties getClientInfo() throws SQLException {270 // TODO Auto-generated method stub271 return null;272 }273 @Override274 public Array createArrayOf(String typeName, Object[] elements) throws SQLException {275 // TODO Auto-generated method stub276 return null;277 }278 @Override279 public Struct createStruct(String typeName, Object[] attributes) throws SQLException {280 // TODO Auto-generated method stub281 return null;282 }283 @Override284 public void setSchema(String schema) throws SQLException {285 // TODO Auto-generated method stub286 287 }288 @Override289 public String getSchema() throws SQLException {290 // TODO Auto-generated method stub291 return null;292 }293 @Override294 public void abort(Executor executor) throws SQLException {295 // TODO Auto-generated method stub296 297 }298 @Override299 public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {300 // TODO Auto-generated method stub301 302 }303 @Override304 public int getNetworkTimeout() throws SQLException {305 // TODO Auto-generated method stub306 return 0;307 }308 }
5、MyDataSource
1 package com.nick.dataSource; 2 3 import java.sql.Connection; 4 import java.util.LinkedList; 5 6 import com.nick.util.JDBCCon; 7 8 public class MyDataSource { 9 10 private static LinkedListpool = new LinkedList ();11 12 static {13 //14 for(int i = 0; i < 5; i++) {15 Connection connection = JDBCCon.getConnection();16 pool.add(connection);17 }18 }19 20 public static Connection getConnection() {21 if(pool.size() == 0) {22 for(int i = 0; i < 5; i++) {23 Connection connection = JDBCCon.getConnection();24 MyConnection myConnection = new MyConnection(connection, pool);25 pool.add(myConnection);26 }27 }28 Connection connection = pool.remove(0);29 return connection;30 31 } 32 }
6、检测代码
1 package com.nick.test; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 6 import org.junit.jupiter.api.Test; 7 8 import com.nick.dataSource.MyDataSource; 9 import com.nick.util.JDBCCon;10 11 public class TestDemo01 {12 13 @Test14 public void t1() {15 Connection connection = null;16 PreparedStatement pstmt = null;17 MyDataSource myDataSource = new MyDataSource();18 try {19 connection = myDataSource.getConnection();20 String sql = "insert into student values (?,?,?,?)";21 PreparedStatement prepareStatement = connection.prepareStatement(sql);22 prepareStatement.setInt(1, 10);23 prepareStatement.setString(2, "吕布");24 prepareStatement.setString(3, "男");25 prepareStatement.setInt(4, 100);26 int rows = prepareStatement.executeUpdate();27 if(rows > 0) {28 System.out.println("OK");29 }else {30 System.out.println("NO");31 }32 } catch (Exception e) {33 // TODO: handle exception34 }35 }36 }
以上即全部代码。