一、通过Driver接口直接连接
站在用户的角度思考问题,与客户深入沟通,找到武安网站设计与武安网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站制作、做网站、企业官网、英文网站、手机端网站、网站推广、主机域名、网络空间、企业邮箱。业务覆盖武安地区。
/** * 通过Driver获取Connection * @return */ public Connection getConnectionByDriver() throws Exception{ String driverClass = "com.MySQL.jdbc.Driver"; String url = "jdbc:mysql:///hdz"; String user = "root"; String password = "123456"; Driver driver = new com.mysql.jdbc.Driver(); Properties info = new Properties(); info.setProperty("driverClass", driverClass); info.setProperty("user", user); info.setProperty("password", password); Connection connection = driver.connect(url, info); return connection; }
二、通过DriverManager直接连接
/** * 通过DriverManager获取Connection * @return * @throws Exception */ public Connection getConnectionByDriverManager() throws Exception{ String url = "jdbc:mysql:///hdz"; String user = "root"; String password = "123456"; Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection(url, user, password); return connection; }
/** * 通过把参数写在配置文件的方式获取Connection * @return * @throws Exception */ public Connection getConectionByProperties() throws Exception{ InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties"); Properties info = new Properties(); info.load(inputStream); String url = info.getProperty("url"); Class.forName("com.mysql.jdbc.Driver"); Connection connection = DriverManager.getConnection(url, info); return connection; } url=jdbc:mysql:///hdz user=root password=123456
三、通过DBCP数据源连接
@Test public void testDbcp() throws Exception { final BasicDataSource basicDataSource = new BasicDataSource(); basicDataSource.setDriverClassName("com.mysql.jdbc.Driver"); basicDataSource.setUrl("jdbc:mysql:///hdz"); basicDataSource.setUsername("root"); basicDataSource.setPassword("123456"); basicDataSource.setInitialSize(2); basicDataSource.setMaxActive(2); basicDataSource.setMinIdle(2); basicDataSource.setMaxWait(2000); Connection connection1 = basicDataSource.getConnection(); System.out.println(connection1); Connection connection2 = basicDataSource.getConnection(); System.out.println(connection2); new Thread(){ @Override public void run() { Connection connection3; try { connection3 = basicDataSource.getConnection(); System.out.println(connection3); } catch (SQLException e) { e.printStackTrace(); } } }.start(); Thread.sleep(3000); connection2.close(); }
或者通过配置文件,BasicDatasourceFactory工厂方式
private DbcpDataSource() { Properties info = new Properties(); InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("dbcp.properties"); try { info.load(inputStream); dataSource = BasicDataSourceFactory.createDataSource(info); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } dbcp.properties driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql:///hdz username=root password=123456 initialSize=5 maxActive=10 minIdle=5 maxWait=5000
四、通过C3P0数据源连接
public class C3p0DataSourceUtils { private DataSource dataSource = null; private static C3p0DataSourceUtils instance = new C3p0DataSourceUtils(); private C3p0DataSourceUtils(){ dataSource = new ComboPooledDataSource("intergalactoApp"); } public static C3p0DataSourceUtils newInstance(){ return instance; } public Connection getConnection() { try { return dataSource.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return null; } }
c3p0-config.xml <?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <!-- This app is massive! --> <named-config name="intergalactoApp"> <property name="acquireIncrement">1</property> <property name="initialPoolSize">1</property> <property name="minPoolSize">1</property> <property name="maxPoolSize">1</property> <property name="maxIdleTimeExcessConnections">1000</property> <!-- intergalactoApp adopts a different approach to configuring statement caching --> <property name="maxStatements">10</property> <property name="maxStatementsPerConnection">5</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql:///hdz</property> <property name="user">root</property> <property name="password">123456</property> </named-config> </c3p0-config>
标题名称:JDBC连接数据库
文章分享:https://www.cdcxhl.com/article38/pidpsp.html
成都网站建设公司_创新互联,为您提供网页设计公司、品牌网站建设、网站制作、营销型网站建设、自适应网站、网站设计
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联