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 use jdbc to connect to Mysql in Java

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

In this issue, the editor will bring you about how to use jdbc to connect to Mysql in Java. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

First, create in the MySQL console

SQL code

Create database test

Use test

Create table user (username varchar (15), password varchar (20))

Insert into user values ('userone','123456')

You can also create it with MySQL-Front

Java code

Package com.dgy.util

Import java.sql.Connection

Import java.sql.DriverManager

Import java.sql.ResultSet

Import java.sql.SQLException

Import java.sql.Statement

Public class TestJDBC {

/ * *

* 1. The driver package used is mysql-connector-java-5.0.8-bin.jar.

* 2. The object that Statement uses to execute static SQL statements and return the results it generates

* by default, only one ResultSet object can be opened per Statement object at a time.

* therefore, if reading one ResultSet object crosses with reading another

* the two objects must be generated by different Statement objects.

* if there is an open current ResultSet object for a statement

* then all execution methods in the Statement interface implicitly close it.

* 3. ResultSet represents the data table of the database result set, which is usually generated by executing statements that query the database.

* the ResultSet object has a pointer to its current data row. Initially, the pointer is placed before the first line.

* the next method moves the pointer to the next line

* because this method returns false when there is no next line in the ResultSet object

* so you can use it in the while loop to iterate over the result set.

* * /

Static Connection conn = null

Public static Connection getConnectionByJDBC () {

Try {

/ / load driver package class

Class.forName (com.mysql.jdbc.Driver "); / / load driver

} catch (ClassNotFoundException e) {

System.out.println ("an exception occurred when loading the driver package! please check!")

E.printStackTrace ()

}

Try {

/ * * establish a jdbc connection, but pay attention to the first parameter of this method

* if a CommunicationsException exception occurs in 127.0.0.1

* you may need to change it to localhost.

* * /

/ / jdbc:mysql://localhost:3306/test,test is a database

Conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/test", "root", "123456")

} catch (SQLException e) {

System.out.println ("exception occurred in the linked database!")

E.printStackTrace ()

}

Return conn

}

Public static void test () {

String sql = "select * from user"

GetConnectionByJDBC ()

Try {

/ / create a jdbc declaration

Statement stmt = conn.createStatement ()

/ / execute query

ResultSet rs = stmt.executeQuery (sql)

While (rs.next ()) {

String username = rs.getString ("username")

String password= rs.getString ("password")

System.out.println (username + "" + password)

}

} catch (SQLException e) {

System.out.println (e.getMessage ())

E.printStackTrace ()

} finally {

/ / Preventive closing of the connection (to avoid the failure of closing the connection in the try statement block when an exception occurs)

Try {

If (conn! = null) conn.close ()

} catch (SQLException e) {

System.out.println (e.getMessage ())

E.printStackTrace ()

}

}

}

Public static void main (String [] args) {

TestJDBC testjdbc = new TestJDBC ()

Testjdbc.test ()

}

}

This is how to use jdbc to connect to Mysql in Java shared by Xiaobian. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, 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.

Share To

Wechat

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

12
Report