In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article shows you how to use JDBC and DBCP to access the database in Java, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
JDBC:
Connection conn = null;Statement stmt = null;ResultSet rs = null;// 1. Loading the driver try {Class.forName ("com.ibm.db2.jcc.DB2Driver"); / * Driver driver = new com.ibm.db2.jcc.DB2Driver (); DriverManager.registerDriver (driver); / / it doesn't make much sense because the class has already created the DB2Driver object when it is loaded and registers * /} catch (Exception e) {e.printStackTrace () with DriverManager. } String url = "jdbc:db2://10.10.38.138:50000/malltest"; String username = "db2inst1"; String password = "db2inst1"; try {/ / 2. Create a database connection conn = DriverManager.getConnection (url, username, password); / / 3. Get the database operation object stmt = conn.createStatement (); / / 4. Operate the database to get the result set rs = stmt.executeQuery ("select * from ly.t_merinf where merid='M0000178'"); / / 5. Process the result set while (rs.next ()) {System.out.println (rs.getString ("mername"));}} catch (SQLException e) {e.printStackTrace ();} finally {/ / close the result set if (rs! = null) {try {rs.close () } catch (SQLException e) {}} / / close the database operator if (stmt! = null) {try {stmt.close ();} catch (SQLException e) {}} / / close the database connection if (conn! = null) {try {conn.close () } catch (SQLException e) {}
DBCP:
/ / 1. Create a connection pool DataSource ds = null;try {Properties prop = new Properties (); / / load the properties file prop.load (DbcpTest.class.getClassLoader (). GetResourceAsStream ("database/dbcp/dbcp.properties")) through the classpath; / / get the data source ds = BasicDataSourceFactory.createDataSource (prop);} catch (IOException e) {e.printStackTrace () } catch (Exception e) {e.printStackTrace ();} Connection conn = null;Statement stmt = null;ResultSet rs = null;try {/ / 2. Get the database connection conn = ds.getConnection (); / / 3. Create the database operation object stmt = conn.createStatement (); / / 4. Operate the database to get the result set rs = stmt.executeQuery ("select * from ly.t_merinf where merid='M0000178'"); / / 5. Process the result set while (rs.next ()) {System.out.println (rs.getString ("mername"));}} catch (SQLException e) {e.printStackTrace ();} finally {/ / close the result set if (rs! = null) {try {rs.close () } catch (SQLException e) {}} / / close the database operator if (stmt! = null) {try {stmt.close ();} catch (SQLException e) {}} / / close the database connection if (conn! = null) {try {conn.close () } catch (SQLException e) {}
Configuration file:
DriverClassName=com.ibm.db2.jcc.DB2Driverurl=jdbc:db2://10.10.38.138:50000/malltestusername=db2inst1password=db2inst1initialSize=3maxActive=5maxIdle=3minIdle=1maxWait=30000
C3P0:
ComboPooledDataSource cpds = new ComboPooledDataSource (); / / load the database driver try {cpds.setDriverClass ("com.ibm.db2.jcc.DB2Driver");} catch (PropertyVetoException E1) {e1.printStackTrace ();} / / set the address, user name and password to access the database cpds.setJdbcUrl ("jdbc:db2://10.10.38.138:50000/malltest"); cpds.setUser ("db2inst1"); cpds.setPassword ("db2inst1") / / set some configurations of C3P0, otherwise use the default values cpds.setMinPoolSize (5); cpds.setAcquireIncrement (5); cpds.setMaxPoolSize (20); cpds.setMaxStatements (180); Connection conn = null;Statement stmt = null;ResultSet rs = null;try {/ / create database connection conn = cpds.getConnection (); / / get database operation object stmt = conn.createStatement () / / manipulate the database to get the result set rs = stmt.executeQuery ("select * from ly.t_merinf where merid='M0000178'"); / / process the result set while (rs.next ()) {System.out.println (rs.getString ("mername"));} catch (SQLException e) {e.printStackTrace () } finally {/ / close the result set if (rs! = null) {try {rs.close ();} catch (SQLException e) {}} / / close the database operation object if (stmt! = null) {try {stmt.close () } catch (SQLException e) {}} / / close database connection if (conn! = null) {try {conn.close ();} catch (SQLException e) {} try {DataSources.destroy (cpds);} catch (SQLException e) {e.printStackTrace () }} the above is how to use JDBC and DBCP to access the database in Java. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.