Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Basic use of JDBC+C3P0+DBCP

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

1. Overview

This article mainly talks about the basic use of JDBC, including the connection of Statement,PreparedStatement,JDBC, the creation of user-created data table by Mysql, the connection and configuration of C3P0, the connection and configuration of DBCP.

The treatment of 2.mysql

The JDBC here uses Mysql as the DBMS, please install Mysql first, click here to download the uninstalled ones, the installation tutorial is here, the author uses version 8.0.17 of Mysql.

(1) create a new user

Casually create a new user, for example, the new user here is aa, and the password is aa123bb.

Create user 'aa'@'localhost' identified by' aa123bb' (2) sets up a data table

Set up data tables and databases for testing.

Create database db;use db;create table db (id int PRIMARY key, name char (20)); (3) user permissions

Authorize the user you just created:

Grant select,update,delete,insert on db.* to 'aa'@'localhost';2.JDBC (1) jar package

Version 8.0.17 is here.

Download each version here

(2) connection

First of all, register the driver, which requires a url, user name and password, which were created in the previous step. Url contains the ip address, port and database name.

Private static final boolean mysqlVersionGreaterThen8 = true;private static final String driver = "com.mysql" + (mysqlVersionGreaterThen8? ".cj": ") +" .jdbc.Driver "; private static final String ip =" 127.0.0.1 "; private static final String port =" 3306 "; private static String databaseName =" db "; private static String url;private static String username =" aa "; private static String password =" k041400r "; private static Connection connection = null Public static Connection getConnection () {try {url = "jdbc:mysql://" + ip + ":" + port + "/" + databaseName; Class.forName (driver); return connection = DriverManager.getConnection (url, username, password);} catch (Exception e) {e.printStackTrace ();} return null;}

Note here that the driver of the old version of mysql is called com.mysql.jdbc.Driver, and the new version is called com.mysql.cj.jdbc.Driver. Then there is the format of url:

Jdbc:mysql://ip:port/database (3) Statement

After obtaining the database connection, use the createStatement method to create a Statement

For select, use Statement's executeQuery (sql), return ResultSet; for update,delete,insert, use Statement's executeUpdate (sql)

Where sql is the sql statement to be executed, a String.

Public void useStatement () {try {useStatementInsert (); useStatementSelect (); useStatementUpdate (); useStatementSelect (); useStatementDelete ();} catch (SQLException e) {e.printStackTrace ();}} public void useStatementInsert () throws SQLException {String sql = "insert into db (id,name) values"; Statement statement = connection.createStatement (); statement.executeUpdate (sql) } public void useStatementDelete () throws SQLException {String sql = "delete from db"; Statement statement = connection.createStatement (); statement.executeUpdate (sql);} public void useStatementSelect () throws SQLException {String sql = "select * from db"; Statement statement = connection.createStatement (); ResultSet resultSet = statement.executeQuery (sql); ResultSetMetaData resultSetMetaData = resultSet.getMetaData (); int count = resultSetMetaData.getColumnCount (); while (resultSet.next ()) {for (int I = 1; I)

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report