In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how java connects to the database". In the daily operation, I believe many people have doubts about how java connects to the database. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "how to connect to the database by java". Next, please follow the editor to study!
Instructions before reading
Java projects need to have corresponding drivers to connect data. You can download the corresponding driver package from the official website.
If you use a maven project, you can add the following dependencies to the pom file:
Mysql mysql-connector-java 8.0.25
Note: the editor uses the 8.0.25 version of the database, so the version number is 8.0.25, please modify different versions.
Connect to the database and basic operations (comments attached to the code):
Import java.sql.*;// imports sql package to jdbc operation public class App {public static void main (String [] args) {String url = "jdbc:mysql://127.0.0.1:3306/"; / / Database host address String database = "W3C"; / / Database name String encoding = "? characterEncoding=UTF-8"; / / Database character set String username = "root" / / the user name of the connection String password = "root"; / / the password of the connection String insertSQL = "insert into newtable values"; / / insert the SQL statement String selectSQL = "select * from newtable"; / / query the SQL statement Connection connection = null; / / initialize the database connection Statement statement = null / / initialize statement try {connection= DriverManager.getConnection (url+database+encoding, username, password); / / create a database connection statement= connection.createStatement () / / create a statement / / statement is an important interface for java to perform database operations, which is used to execute simple sql statements / / Note: use java.sql.Statement, do not accidentally use: com.mysql.jdbc.Statement; statement.execute (insertSQL) / / use the excute () method to create, add, delete, insert and other SQL statements ResultSet result = statement.executeQuery (selectSQL) / / use excuteQuery () to execute the query statement and return the result set to the ResultSet / / data display method. Without introducing while (result.next ()) {/ / using the next method, you can fetch data one by one. If you want to retrieve all of them, you can first store int id=result.getInt (1) in an array. / / get the data of the first column String user=result.getString (2); / / get the data of the second column String pwd=result.getString (3); / / get the data System.out.println of the third column ("number:" + id+ ", user name:" + user+ ", password:" + pwd) System.out.println ("- -");}} catch (SQLException e) {e.printStackTrace () } finally {/ / the connection to the database is limited. After the relevant operations are completed, form the good habit of shutting down the database / / first close Statement if (statement! = null) try {statement.close () / / close statement} catch (SQLException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} / / close Connection if (connection! = null) try {connection.close () / / close database connection} catch (SQLException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} at this point, the study on "how java connects to the database" is over. I hope you can solve your 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.
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.