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 write the detailed code of Java connecting to Mysql database

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Java connection Mysql database detailed code how to write, in view of this problem, this article describes in detail the corresponding analysis and solutions, hoping to help more partners who want to solve this problem to find a more simple and easy way.

After a period of time did not even connect to the database, the code have forgotten, read the next quite messy online, it is better to record their own. The code here is mainly to connect to the database and display data, there is no specific database operation.

The first step is, of course, to import the jar package of mysql, create a new folder in the java file, name it lib here, and then copy the jar package into it.

Then right-click the jar package and select build path to add the package to the library

And then the code is implemented.

Package _ 9 / 3 String JDBC_DRIVER / import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Scanner;import com.mysql.jdbc.Driver;import com.mysql.jdbc.Statement;public class Scr {public static void main (String [] args) {/ / JDBC driver name String JDBC_DRIVER = "com.mysql.jdbc.Driver" / / Database URL: where tt is the database name String JDBC_URL = "jdbc:mysql://localhost:3306/tt?useSSL=false&serverTimezone=UTC"; / / the user name and password of the database String USER = "root"; String PASS = "admin123"; / / the connection object can be obtained through the DriverManager class to access the database Connection connection = null;//. Get the result object through Connection to execute the static SQL statement Statement statement = null. Try {/ / registers JDBC driver Class.forName (JDBC_DRIVER); / / Database connection: pass in three parameters: database URL, user name, user password, instantiate connection object connection = DriverManager.getConnection (JDBC_URL,USER,PASS); / / instantiate statement object statement = (Statement) connection.createStatement () / / define the database query statement: query the name and sex columns of data in the aa table String sql = "SELECT name,sex FROM aa"; / / execute the query statement ResultSet rSet = statement.executeQuery (sql); / / expand the queried data while (rSet.next ()) {/ / the parameters in the getString () method correspond to the column name String get_name = rSet.getString ("name") in the database table String get_sex = rSet.getString ("sex"); / / output data System.out.println ("name:" + get_name); System.out.print ("gender:" + get_sex);} / / turn off object rSet.close (); statement.close (); connection.close ();} catch (ClassNotFoundException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} catch (SQLException e) {/ / TODO Auto-generated catch block e.printStackTrace () }}}

Display effect:

In the whole process of connecting to the database, the implementation of the code is not difficult, and the more verbose part is the use of the database, such as the opening of the database, the insertion of database data, the use of the database graphical interface, and so on. I'm tired.

The answer to the question about how to write the detailed code of Java connection Mysql database is shared here. I hope the above content can be of some help to everyone. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

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