In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "java connection mysql failure how to do", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "java connection mysql failure how to do" this article.
Find a problem
Someone must have found that the connection to mysql failed, then couldn't find the problem, and there was another big error, as shown in the following figure.
Solution process
1. First query your java version and type java-version in the cmd runtime window.
two。 Query the downloaded mysql-connector-java version
If it is the same as the java version I pictured above, then you need to use the latest jar package.
That is, the code above mysql-connector-java-8.0.26.
If the version of jar package is too low, download it again.
1. First of all, let's open mysql's official website: https://www.mysql.com/
two。 Click to select the DOWNLOADS option:
3. Click to select MySQL Community:
4. Click to select the Connector/J option:
5. Select GO to download page
6. Download download
7. Choose to skip the download directly
3. Next, java connects to the database.
The first step is to download a mysql database-driven jar package on 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.
Just copying here is not enough, you need to add the JAR package to the project's build path variable. Right side of the mouse-"build path- > configure build path.. Click Add jars in the pop-up interface to select the driver jar package and add it to the build path variable.
3. Now there is a library student in the database, in which there is a table admit with two columns, which can be done in any way.
4. And then you write the code.
Write in this class
Package mysqlconnect;import java.sql.*;public class mysqlconnect {public static void main (String [] args) {/ / load database driver try {Class.forName ("com.mysql.cj.jdbc.Driver");} catch (ClassNotFoundException e) {/ / TODO Auto-generated catch block e.printStackTrace () } / / set user information and url String user = "root"; String password = "123456"; / * password * / String url = "jdbc:mysql://localhost:3306/student"; / * Library name in jdbc:mysql://localhost:3306/ database * / get database object connection Connection conn = null Try {conn = DriverManager.getConnection (url,user,password);} catch (SQLException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} / / get the statement object Statement stat = null; try {stat = conn.createStatement () through the database object connection } catch (SQLException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} / / execute the sql statement String sql = "insert into admit values ('123'); / * add value to the admit table in the student database * / try {stat.executeUpdate (sql) } catch (SQLException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} / / release the connection resource try {stat.close ();} catch (SQLException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} try {conn.close () } catch (SQLException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} these are all the contents of the article "what to do if java connects to mysql failed". 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.