Home Database navicat How to connect to navicat in java

How to connect to navicat in java

Aug 20, 2019 pm 01:47 PM
java navicat connect

How to connect to navicat in java

In actual applications, most of them need to use the database to query and operate data, because the database supports powerful SQL statements and can perform transaction processing. Next, I will introduce how to use the API and database provided by JDBC for information delivery in Java applications.

First of all, you need to install the MySQl database, install the installed database, establish the database, and create tables, you also need to operate the database through JDBC.

Java operates the database by using the API provided by JDBC. JDBC operates different databases only with different connection methods. After the application using JDBC establishes contact with the database, it can operate it.

Using JDBC requires the following operations:

·Establish a connection with the database

·Send SQL statements to the connected database

·Processing the results returned by SQL statements

After the MySQL database service is started, a connection must first be established with the database on the database server. Java uses JDBC to call the local JDBC-database driver and establish a connection with the corresponding database. The Java running environment converts the JDBC database driver into a special protocol used by DBMS (database management system) to implement information interaction with specific DBMS, as shown below:

How to connect to navicat in java

Using JDBC- There are two steps required to establish a connection between the database driver and the database:

(1) Load the JDBC-database driver

(2) Establish a connection with the specified database

Download first JDBC-MySQL database driver, JDBC download URL:

https://dev.mysql.com/downloads/file/?id=474258. Unzip the downloaded zip file, find the mysql-connector-java-5.1.45-bin.jar file inside, and copy it to the JDK extension directory, for example, mine is in the D:StudyJava1.8.0jrelibext directory.

How to connect to navicat in java

Related recommendations: "Navicat for mysql graphic tutorial"

At the same time, because there will be an additional JRE when installing the JDK , it is best to copy the mysql-connector-java-5.1.45-bin.jar file to that directory, mine is the D:StudyJRE1.8.0libext directory, to ensure that the environment is enabled in time to run the program, and there will be the necessary drivers.

How to connect to navicat in java

The next step is to load the JDBC-MySQL database driver. The code is as follows:

try {  
Class.forName("com.mysql.jdbc.Driver");  
} 
catch (Excepton e) { }
Copy after login

The MySQL database driver is encapsulated in the Driver class, and the package name is com .mysql.jdbc, it is not a class in the Java runtime environment class library, so it needs to be placed in the extension directory of jre.

The DriverManager class in the java.sql package has two class methods (static methods) for establishing connections:

·Connection getConnection(java.lang.String ,java.lang.String,java.lang.String)

·Connection getConnection(java.lang.String)

Find MySQL Notifier on the computer and run it, Then right-click its icon on the taskbar, select MySQL57-Stopped-->Start to start the database

How to connect to navicat in java

Then open Navicat for MySQL, open the connection, and establish a connection with the database server Finally, right-click the connection name and select the "New Database" command, fill in the corresponding information in the pop-up dialog box, as shown below, and create a database named Study.

How to connect to navicat in java

After establishing the database, right-click "Table" under Study and select "New Table", fill in the corresponding column names and various attributes of the column names,

How to connect to navicat in java

Click save, enter the table name to save, then double-click the table name, fill in the corresponding information, add a new row, and click " ".

How to connect to navicat in java

#Next, use a small program to test the query operation on the database.

Create a new Java project, the code is as follows:

import 
java.sql.*;public class MysqlTest { public static void main(String[]  
args) { //加载JDBC-MySQL驱动 try { Class.forName("com.mysql.jdbc.Driver"); } 
catch (Exception e) {}  
//同数据库建立连接,其中ip地址后的3306是端口号,study是数据库名称,加上用户名和密码等信息 //设置useSSL为true  
//如果表中记录有汉字,请加上characterEncoding参数,取值为gb2312或utf-8 Connection con =  
null; String uri =  
"jdbc:mysql://192.168.199.240:3306/study?user=root&password=****&useSSL=true&characterEncoding=utf-8"; 
try { con = DriverManager.getConnection(uri); //连接代码 } catch  
(SQLException e) { System.out.println(e); } //向数据库发送SQL查询语句 try {  
Statement sql = con.createStatement(); //声明并创建SQL语句对象 ResultSet rs =  
sql.executeQuery("SELECT * FROM test"); //查询test表  
System.out.println("查询结果:"); while (rs.next()) { //循环读取表test每行的数据并输出  
String number = rs.getString(1); String name = rs.getString(2); Date  
date = rs.getDate(3); float height = rs.getFloat(4);  
System.out.printf("%s   ",number); System.out.printf("%s    ",name);  
System.out.printf("%s   ",date); System.out.printf("%s  ",height);  
System.out.println(); } con.close(); //关闭数据库连接 } catch (SQLException e) { 
 System.out.println(e); } }}
Copy after login

Run this program, the output result is as follows:

How to connect to navicat in java

At this point, Java connects to the MySQL database Successfully implemented sequential query.

The above is the detailed content of How to connect to navicat in java. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use navicat keygen patch How to use navicat keygen patch Apr 09, 2025 am 07:18 AM

Navicat Keygen Patch is a key generator that activates Navicat Premium, allowing you to use the full functionality of the software without purchasing a license. How to use: 1) Download and install Keygen Patch; 2) Start Navicat Premium; 3) Generate the serial number and activation code; 4) Copy the key; 5) Activate Navicat Premium, and Navicat Premium can be activated.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

How to see if navicat is activated How to see if navicat is activated Apr 09, 2025 am 07:30 AM

How to check if Navicat is activated: View the Registration Information section in the "About Navicat" of the Help menu: Activated: Show valid registration information (name, organization, expiration date) Not activated: Show "Not Registered" or "Register Information Not Available" Check the activation icon in the toolbar: The green icon indicates that the active observation trial period countdown: The trial version will show the countdown at startup, the activated version will not view feature limitations: The trial version may limit advanced features, and the activated version will unlock all functions

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

See all articles