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

How to download JDBC and connect to MySQL

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to download JDBC and connect MySQL". In daily operation, I believe many people have doubts about how to download JDBC and connect MySQL. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "how to download JDBC and connect MySQL". Next, please follow the editor to study!

Download JDBC and connect to MySQL steps

1. Install MYSQL

2. Set the JDBC driver and TOMCAT-HOME/COMMON/LIB

3. Arrange JSP

MySQL test

4. If an IO exception occurs due to the low version of the JDBC driver, go back to MYSQL to download the * * driver. In addition, the org.gjt.mm.mysql under JDBC DRIVER FOR MYSQL is reserved for compatibility with the old version of mySQL?

5. Basic operation of JDBC

Import java.sql.*;import java.io.*;... Class.forName ("target database jdbc driver"); Connection con = DriverManager.getConnection ("connection string"); Statement stmt = con.createStatement (); ResultSet result = stmt.executeQuery ("sql command"); stmt.executeUpdate ("sql command"); result.close (); stmt.close (); con.close ()

To download the JDBC driver, you need to add the CLASSPATH environment variable

The method of JSP driving MySQL Database through JDBC

JDBC connection MySQL

Load and register JDBC drivers

Class.forName ("com.mysql.jdbc.Driver")

Class.forName (com.mysql.jdbc.Driver) .newInstance ()

JDBC URL defines the connection between the driver and the data source

Standard syntax:

::

JDBC URL format of MySQL:

Jdbc:mysql// [hostname] [: port] / [dbname] [? param1=value1] [& param2=value2]... .

Example: jdbc:mysql://localhost:3306/sample_db?user=root&password=your_password

Common parameters:

User user name

Password password

AutoReconnect failed to go online, whether to go back online (true/false)

Number of maxReconnect attempts to go back online

Interval between initialTimeout attempts to come back online

MaxRows returns the number of * * rows

Whether useUnicode uses Unicode font encoding (true/false)

What kind of coding for characterEncoding (GB2312/UTF-8/ … )

Whether relaxAutocommit is automatically submitted (true/false)

The name of the capitalizeTypeNames data definition is represented in uppercase

Establish a connection object

String url= "jdbc:mysql://localhost:3306/sample_db?user=root&password=your_password"; Connection con = DriverManager.getConnection (url)

Create a SQL declarative object (Statement Object)

Statement stmt = con.createStatement ()

Execute SQL statement

ExecuteQuery () String query = "select * from test"; ResultSet rs=stmt.executeQuery (query)

Result set ResultSet

While (rs.next ()) {rs.getString (1); rs.getInt (2);} executeUpdate () String upd= "insert into test (id,name) values"; int con=stmt.executeUpdate (upd); execute () so far, the study on "how to download JDBC and connect to MySQL" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report