In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
First, connect directly through the Driver interface
/ * obtain 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) through Driver Info.setProperty ("password", password); Connection connection = driver.connect (url, info); return connection;}
Second, connect directly through DriverManager
/ * obtain 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") through DriverManager; Connection connection = DriverManager.getConnection (url, user, password); return connection } / * get 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") by writing parameters in the configuration file Connection connection = DriverManager.getConnection (url, info); return connection;} url=jdbc:mysql:///hdzuser=rootpassword=123456
Third, connect through DBCP data source
Testpublic 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 ();}
Or through the configuration file, BasicDatasourceFactory factory mode
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.propertiesdriverClassName=com.mysql.jdbc.Driverurl=jdbc:mysql:///hdzusername=rootpassword=123456initialSize=5maxActive=10minIdle=5maxWait=5000
Fourth, connect through C3P0 data source
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 1 1 1 1000 10 5com.mysql.jdbc.Driverjdbc:mysql:///hdzroot123456
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.