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:
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.
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.
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) { }
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
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.
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,
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 " ".
#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); } }}
Run this program, the output result is as follows:
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!