package tk.dong.connection.util;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.sql.Array;import java.sql.Blob;import java.sql.CallableStatement;import java.sql.Clob;import java.sql.Connection;i
package tk.dong.connection.util; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.sql.Array; import java.sql.Blob; import java.sql.CallableStatement; import java.sql.Clob; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.NClob; import java.sql.PreparedStatement; import java.sql.SQLClientInfoException; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; import java.sql.SQLWarning; import java.sql.SQLXML; import java.sql.Savepoint; import java.sql.Statement; import java.sql.Struct; import java.util.LinkedList; import java.util.Map; import java.util.Properties; import java.util.concurrent.Executor; import java.util.logging.Logger; import javax.sql.DataSource; //这是同过增强Connection类[重写了close的方法]实现的从连接池取出连接并放回连接 public class JdbcPool implements DataSource { // 创建连接池,用的是LinkList, private static LinkedList<Connection> connections = new LinkedList<Connection>(); // 通过静态初始化块创建,当程序进行初始化的时候就会触发 static { // 将连接数据库的配置文件读入到流中 InputStream inStream = JdbcPool.class.getClassLoader().getResourceAsStream( "jdbc.properties"); Properties properties = new Properties(); try { properties.load(inStream); // 获取连接数据库的驱动(根据property属性文件中的属性获取驱动) Class.forName(properties.getProperty("driverClassName")); for (int i = 0; i < 10; i++) { // 获取conn连接连接对象 Connection conn = DriverManager.getConnection( properties.getProperty("url"), properties.getProperty("user"), properties.getProperty("pass")); // 这是在装入连接池的时候进行的操作处理,将connection中的close进行改写 MyConnection myConnection = new MyConnection(conn, connections); // 将处理后的连接对象存入连接池 connections.add(myConnection); System.out.println("连接池已经加入了::::::::::" + connections.size() + "::::::::::个链接对象"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 这两个是从连接池中获取连接的方法 @Override public Connection getConnection() throws SQLException { //生命连接对象 Connection conn=null; if(connections.size()>0){ //取出链表中的第一个对象赋值给临时的连接对象 conn=connections.removeFirst(); System.out.println("又一个连接对象被拿走:::::::连接池还有"+connections.size()+"个连接对象"); } return conn; } @Override public Connection getConnection(String username, String password) throws SQLException { // TODO Auto-generated method stub return null; } @Override public PrintWriter getLogWriter() throws SQLException { // TODO Auto-generated method stub return null; } @Override public void setLogWriter(PrintWriter out) throws SQLException { // TODO Auto-generated method stub } @Override public void setLoginTimeout(int seconds) throws SQLException { // TODO Auto-generated method stub } @Override public int getLoginTimeout() throws SQLException { // TODO Auto-generated method stub return 0; } @Override public Logger getParentLogger() throws SQLFeatureNotSupportedException { // TODO Auto-generated method stub return null; } @Override public <T> T unwrap(Class<T> iface) throws SQLException { // TODO Auto-generated method stub return null; } @Override public boolean isWrapperFor(Class<?> iface) throws SQLException { // TODO Auto-generated method stub return false; } } // 增强的Connection 连接对类 // 装饰器模式 // 1.首先看需要被增强对象继承了什么接口或父类,编写一个类也去继承这些接口或父类。 class MyConnection implements Connection { // 2.在类中定义一个变量,变量类型即需增强对象的类型。 // 用来接收连接池 private LinkedList<Connection> connections; // 用来接收连接对象 private Connection conn; // 构造器为 public MyConnection(Connection conn, LinkedList<Connection> connections) { this.conn = conn; this.connections = connections; } // 我只用到这个方法所以我只写这个方法 @Override public void close() throws SQLException { // 将不用的连接再次放入连接池 connections.add(conn); System.out.println("有一个连接对象用完了,已经返回连接池,现在连接池中还有==="+connections.size()); } // 下面的方法用不到,所以就不写内容了 @Override public <T> T unwrap(Class<T> iface) throws SQLException { // TODO Auto-generated method stub return null; } @Override public boolean isWrapperFor(Class<?> iface) throws SQLException { // TODO Auto-generated method stub return false; } @Override public Statement createStatement() throws SQLException { // TODO Auto-generated method stub return null; } @Override public PreparedStatement prepareStatement(String sql) throws SQLException { // TODO Auto-generated method stub return null; } @Override public CallableStatement prepareCall(String sql) throws SQLException { // TODO Auto-generated method stub return null; } @Override public String nativeSQL(String sql) throws SQLException { // TODO Auto-generated method stub return null; } @Override public void setAutoCommit(boolean autoCommit) throws SQLException { // TODO Auto-generated method stub } @Override public boolean getAutoCommit() throws SQLException { // TODO Auto-generated method stub return false; } @Override public void commit() throws SQLException { // TODO Auto-generated method stub } @Override public void rollback() throws SQLException { // TODO Auto-generated method stub } @Override public boolean isClosed() throws SQLException { // TODO Auto-generated method stub return false; } @Override public DatabaseMetaData getMetaData() throws SQLException { // TODO Auto-generated method stub return null; } @Override public void setReadOnly(boolean readOnly) throws SQLException { // TODO Auto-generated method stub } @Override public boolean isReadOnly() throws SQLException { // TODO Auto-generated method stub return false; } @Override public void setCatalog(String catalog) throws SQLException { // TODO Auto-generated method stub } @Override public String getCatalog() throws SQLException { // TODO Auto-generated method stub return null; } @Override public void setTransactionIsolation(int level) throws SQLException { // TODO Auto-generated method stub } @Override public int getTransactionIsolation() throws SQLException { // TODO Auto-generated method stub return 0; } @Override public SQLWarning getWarnings() throws SQLException { // TODO Auto-generated method stub return null; } @Override public void clearWarnings() throws SQLException { // TODO Auto-generated method stub } @Override public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { // TODO Auto-generated method stub return null; } @Override public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { // TODO Auto-generated method stub return null; } @Override public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { // TODO Auto-generated method stub return null; } @Override public Map<String, Class<?>> getTypeMap() throws SQLException { // TODO Auto-generated method stub return null; } @Override public void setTypeMap(Map<String, Class<?>> map) throws SQLException { // TODO Auto-generated method stub } @Override public void setHoldability(int holdability) throws SQLException { // TODO Auto-generated method stub } @Override public int getHoldability() throws SQLException { // TODO Auto-generated method stub return 0; } @Override public Savepoint setSavepoint() throws SQLException { // TODO Auto-generated method stub return null; } @Override public Savepoint setSavepoint(String name) throws SQLException { // TODO Auto-generated method stub return null; } @Override public void rollback(Savepoint savepoint) throws SQLException { // TODO Auto-generated method stub } @Override public void releaseSavepoint(Savepoint savepoint) throws SQLException { // TODO Auto-generated method stub } @Override public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { // TODO Auto-generated method stub return null; } @Override public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { // TODO Auto-generated method stub return null; } @Override public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { // TODO Auto-generated method stub return null; } @Override public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { // TODO Auto-generated method stub return null; } @Override public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { // TODO Auto-generated method stub return null; } @Override public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException { // TODO Auto-generated method stub return null; } @Override public Clob createClob() throws SQLException { // TODO Auto-generated method stub return null; } @Override public Blob createBlob() throws SQLException { // TODO Auto-generated method stub return null; } @Override public NClob createNClob() throws SQLException { // TODO Auto-generated method stub return null; } @Override public SQLXML createSQLXML() throws SQLException { // TODO Auto-generated method stub return null; } @Override public boolean isValid(int timeout) throws SQLException { // TODO Auto-generated method stub return false; } @Override public void setClientInfo(String name, String value) throws SQLClientInfoException { // TODO Auto-generated method stub } @Override public void setClientInfo(Properties properties) throws SQLClientInfoException { // TODO Auto-generated method stub } @Override public String getClientInfo(String name) throws SQLException { // TODO Auto-generated method stub return null; } @Override public Properties getClientInfo() throws SQLException { // TODO Auto-generated method stub return null; } @Override public Array createArrayOf(String typeName, Object[] elements) throws SQLException { // TODO Auto-generated method stub return null; } @Override public Struct createStruct(String typeName, Object[] attributes) throws SQLException { // TODO Auto-generated method stub return null; } @Override public void setSchema(String schema) throws SQLException { // TODO Auto-generated method stub } @Override public String getSchema() throws SQLException { // TODO Auto-generated method stub return null; } @Override public void abort(Executor executor) throws SQLException { // TODO Auto-generated method stub } @Override public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException { // TODO Auto-generated method stub } @Override public int getNetworkTimeout() throws SQLException { // TODO Auto-generated method stub return 0; } }测试代码
package tk.dong.connectionPool.test; import java.sql.Connection; import java.sql.SQLException; import org.junit.Test; import tk.dong.connection.util.JdbcPool; public class TestJdbcPool { @Test public void jdbcPool() throws SQLException { // 创建连接池对象 JdbcPool jdbcPool = new JdbcPool(); // 从连接池中获取连接对象 jdbcPool.getConnection(); // 多次获取连接对象 jdbcPool.getConnection(); jdbcPool.getConnection(); Connection conn = jdbcPool.getConnection(); // 归还用完的连接对象 conn.close(); // 再次获取连接对象 jdbcPool.getConnection(); jdbcPool.getConnection(); jdbcPool.getConnection(); jdbcPool.getConnection(); } }测试输出结果
连接池已经加入了::::::::::1::::::::::个链接对象 连接池已经加入了::::::::::2::::::::::个链接对象 连接池已经加入了::::::::::3::::::::::个链接对象 连接池已经加入了::::::::::4::::::::::个链接对象 连接池已经加入了::::::::::5::::::::::个链接对象 连接池已经加入了::::::::::6::::::::::个链接对象 连接池已经加入了::::::::::7::::::::::个链接对象 连接池已经加入了::::::::::8::::::::::个链接对象 连接池已经加入了::::::::::9::::::::::个链接对象 连接池已经加入了::::::::::10::::::::::个链接对象 又一个连接对象被拿走:::::::连接池还有9个连接对象 又一个连接对象被拿走:::::::连接池还有8个连接对象 又一个连接对象被拿走:::::::连接池还有7个连接对象 又一个连接对象被拿走:::::::连接池还有6个连接对象 有一个连接对象用完了,已经返回连接池,现在连接池中还有===7 又一个连接对象被拿走:::::::连接池还有6个连接对象 又一个连接对象被拿走:::::::连接池还有5个连接对象 又一个连接对象被拿走:::::::连接池还有4个连接对象 又一个连接对象被拿走:::::::连接池还有3个连接对象

MySQL convient aux débutants pour acquérir des compétences de base de données. 1. Installez les outils MySQL Server et Client. 2. Comprendre les requêtes SQL de base, telles que SELECT. 3. 掌握数据操作: : 创建表、插入、更新、删除数据。 4. 学习高级技巧: : 子查询和窗口函数。 5. 调试和优化: : 检查语法、使用索引、避免 Sélectionner * , 并使用 Limite。

MySQL gère efficacement les données structurées par la structure de la table et la requête SQL, et met en œuvre des relations inter-tableaux à travers des clés étrangères. 1. Définissez le format de données et tapez lors de la création d'une table. 2. Utilisez des clés étrangères pour établir des relations entre les tables. 3. Améliorer les performances par l'indexation et l'optimisation des requêtes. 4. Bases de données régulièrement sauvegarde et surveillent régulièrement la sécurité des données et l'optimisation des performances.

MySQL est un système de gestion de base de données relationnel open source qui est largement utilisé dans le développement Web. Ses caractéristiques clés incluent: 1. Prend en charge plusieurs moteurs de stockage, tels que InNODB et Myisam, adaptés à différents scénarios; 2. Fournit des fonctions de réplication à esclave maître pour faciliter l'équilibrage de la charge et la sauvegarde des données; 3. Améliorez l'efficacité de la requête grâce à l'optimisation des requêtes et à l'utilisation d'index.

SQL est utilisé pour interagir avec la base de données MySQL pour réaliser l'ajout de données, la suppression, la modification, l'inspection et la conception de la base de données. 1) SQL effectue des opérations de données via des instructions SELECT, INSERT, UPDATE, DELETE; 2) Utiliser des instructions Create, Alter, Drop pour la conception et la gestion de la base de données; 3) Les requêtes complexes et l'analyse des données sont mises en œuvre via SQL pour améliorer l'efficacité de la prise de décision commerciale.

Les opérations de base de MySQL incluent la création de bases de données, les tables et l'utilisation de SQL pour effectuer des opérations CRUD sur les données. 1. Créez une base de données: CreatedAtAbaseMy_First_DB; 2. Créez un tableau: CreateTableBooks (idIntauto_inCmentPrimaryKey, TitleVarchar (100) notnull, AuthorVarchar (100) notnull, publied_yearint); 3. Données d'insertion: INSERTINTOBOOKS (titre, auteur, publié_year) VA

Le rôle principal de MySQL dans les applications Web est de stocker et de gérer les données. 1.MySQL traite efficacement les informations utilisateur, les catalogues de produits, les enregistrements de transaction et autres données. 2. Grâce à SQL Query, les développeurs peuvent extraire des informations de la base de données pour générer du contenu dynamique. 3.MySQL fonctionne basé sur le modèle client-serveur pour assurer une vitesse de requête acceptable.

Les étapes pour construire une base de données MySQL incluent: 1. Créez une base de données et une table, 2. Insérer des données et 3. Conduisez des requêtes. Tout d'abord, utilisez les instructions CreateDatabase et CreateTable pour créer la base de données et la table, puis utilisez l'instruction InsertInto pour insérer les données, et enfin utilisez l'instruction SELECT pour interroger les données.

MySQL convient aux débutants car il est facile à utiliser et puissant. 1.MySQL est une base de données relationnelle et utilise SQL pour les opérations CRUD. 2. Il est simple à installer et nécessite la configuration du mot de passe de l'utilisateur racine. 3. Utilisez l'insertion, la mise à jour, la suppression et la sélection pour effectuer des opérations de données. 4. OrderBy, où et jointure peut être utilisé pour des requêtes complexes. 5. Le débogage nécessite de vérifier la syntaxe et d'utiliser Expliquez pour analyser la requête. 6. Les suggestions d'optimisation incluent l'utilisation d'index, le choix du bon type de données et de bonnes habitudes de programmation.


Outils d'IA chauds

Undresser.AI Undress
Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover
Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool
Images de déshabillage gratuites

Clothoff.io
Dissolvant de vêtements AI

AI Hentai Generator
Générez AI Hentai gratuitement.

Article chaud

Outils chauds

Télécharger la version Mac de l'éditeur Atom
L'éditeur open source le plus populaire

MantisBT
Mantis est un outil Web de suivi des défauts facile à déployer, conçu pour faciliter le suivi des défauts des produits. Cela nécessite PHP, MySQL et un serveur Web. Découvrez nos services de démonstration et d'hébergement.

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser