search
HomeJavaJavagetting StartedHow to connect to database in java

How to connect to database in java

Nov 11, 2019 am 11:02 AM
javadatabaseconnect

How to connect to database in java

1. Download a mysql database driver jar package from the Internet, similar to mysql-connector-java-5.1.20-bin.jar. Create a new JAVA project, create a lib folder under the project, and copy the driver jar file to the lib folder

How to connect to database in java

#2. Add the JAR package to the project's build in the path variable. Right side of the mouse-》build path->configure build path.. Click Add jars in the pop-up interface, select the driver jar package and add it to the build path variable

How to connect to database in java

3 , create a JAVA file and write the code to connect to the database. Four variables need to be declared (static, generally not modified). url is the network address of the database and its name. name is the name of the driver (different databases are different, fixed writing methods defined by others), user database user name password database connection password

How to connect to database in java

4. Create a Connection object through the database connection, The constructor of the TestDBHelper class passes in the sql statement, so that when using it, you can directly use the New object and then pass in the SQL statement for execution. Remember to write a method to close the connection. The connection must be closed after each access to the database

How to connect to database in java

5. Write the main method to test the database connection. The result set of the SQL statement execution is placed in the ResultSet In the object, if you want to get the contents inside, you need to loop out them one by one. Remember to close the database connection after use

How to connect to database in java

#6. Check the output information of the console and compare it with the information in the database table to see if it is consistent. In addition, my table here only has two fields. If there are multiple fields, just follow ret.getString(2); ret.getString(3); and take them from the back.

How to connect to database in java

Recommended tutorial: Java tutorial

The above is the detailed content of How to connect to database in java. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment