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 create a database and connect to JDBC with Navicat

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article introduces the knowledge of "how to use Navicat to create a database and connect to JDBC". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Navicat creates a database

1. First install the database visualization software: Navicat for MySQL

(that's the one below) I don't have to say much about this. I need my own Baidu tutorial.

two。 Establish a connection

Enter the connection name, user name, password, and then OK can create a new connection.

So the new connection is established.

3. Create a database

Right-click the Test connection you just created, then New Database, and create the database, which is the following interface

Enter the database name, select the encoding format, OK

Click Test and you will find that there is already the database databasetest you just created, and the database has been created successfully

4. Create a database table

After clicking databasetest, right-click Table, select New Table, and create a new table.

The interface for the new table is as follows, enter the header content, as well as the data type, set the primary key, and so on.

Save the table and enter a table name

When saving, it is found that the setting of the primary key can be NULL, and the error will be prompted. Change it and then save it.

After creating the table, we found that there is a new table under the Table.

5. Add content to the table

You can add the contents of the database table yourself.

That's good, that's all for the simple operation in Navicat.

Let's talk about the operation in the Java code.

Second, JDBC connection Java code

1. Java code

Paste the code here first. There are some problems I have encountered, which will be described later.

Public class ConnMySQL {public static void main (String [] args) throws Exception {/ / load driver Class.forName ("com.mysql.cj.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/databasetest?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"; String user = "root"; String password = "root"; try (/ / obtain database connection with DriverManager / / Connection returned represents the connection between Java program and database Connection conn = DriverManager.getConnection (url, user, password) / use Connection to create a Statement object Statement stmt = conn.createStatement (); / / execute the SQL statement ResultSet rs = stmt.executeQuery ("select * from student")) {while (rs.next ()) {String number = rs.getString ("number"); String name = rs.getString ("name"); int age = rs.getInt ("age"); System.out.println (number + "-" + name + "- -" + age);}}

two。 Error: Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.

After checking the reason, it is found that it is because one package is missing: mysql-connector-java-x.x.x-bin.jar; (Note: look at your MySQL version and choose the appropriate installation package)

Put a link here, which is 8.0.15. You can download it if you need it.

Link: https://pan.baidu.com/s/1CZIPnDzn5XUTggQ-M05W1Q

Extraction code: kdev

After downloading and decompressing the package, install it as follows:

So the problem is solved.

3. Error: Exception in thread "main" java.sql.SQLException: The server time zone value 'assigned host ±host ±error ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

Yes, it's such a long mistake, mm-hmm. no, no, no. Although we do not know the specific reason, but after checking, the solution is as follows:

Many of the URL I've seen before are written like this:

String url = "jdbc:mysql://localhost:3306/databasetest"

Now change it and change it like this:

String url = "jdbc:mysql://localhost:3306/databasetest?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"

The problem will be solved.

You can output the contents of the database table normally.

This is the end of "how to use Navicat to create a database and connect to JDBC". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Database

Wechat

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

12
Report