In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how Java uses JDBC to connect to MySQL database needs driver package, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Java needs a driver package to connect to the MySQL database using JDBC.
The latest version of the download address is: http://dev.mysql.com/downloads/connector/j/, decompress to get the jar library file, and then import the library file in the corresponding project.
1. Create test data
Create a table in MySQL with the following structure:
Create table `w` (
`id`int (11) not null auto_increment
`name` char (20) not null
`url`varchar (255) not null
Primary key (`id`)
) engine=innodb default charset=utf8
Insert into `w` values ('1Qing,' google', 'https://www.google.cm/');
Insert into `w` values ('2Qing,' Taobao', 'https://www.taobao.com/');
two。 Connect to the database
Package com.run.test
Import java.sql.*
Public class MySQLDemo {
/ / JDBC driver name and database URL
Static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"
Static final String DB_URL = "jdbc:mysql://localhost:3306/sss"
/ / the user name and password of the database, according to your own settings
Static final String USER = "root"
Static final String PASS = "123456"
Public static void main (String [] args) {
Connection conn = null
Statement stmt = null
Try {
/ / Register the JDBC driver
Class.forName (JDBC_DRIVER)
/ / Open the link
Conn = DriverManager.getConnection (DB_URL,USER,PASS)
/ / execute query
Stmt = conn.createStatement ()
String sql
Sql = "SELECT name, url FROM w"
ResultSet rs = stmt.executeQuery (sql)
/ / expand the result set database
While (rs.next ()) {
/ / retrieve by field
String name = rs.getString ("name")
String url = rs.getString ("url")
/ / output data
System.out.print ("site name:" + name)
System.out.print ("site URL:" + url)
System.out.print ("\ n")
}
/ / close after completion
Rs.close ()
Stmt.close ()
Conn.close ()
} catch (SQLException se) {
/ / handle JDBC errors
Se.printStackTrace ()
} catch (Exception e) {
/ / handle Class.forName errors
E.printStackTrace ()
} finally {
/ / close resources
Try {
If (stmtasking null) stmt.close ()
} catch (SQLException se2) {
}
Try {
If (connexual null) conn.close ()
} catch (SQLException se) {
Se.printStackTrace ()
}
}
}
}
The output of the above example is as follows:
The above is all the contents of the article "how to use JDBC to connect to MySQL database needs driver package for Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.