Steps to connect to Navicat in Java Web: Add MySQL driver Create connection properties Execute query
How to connect in Java Web Navicat
Step 1: Add MySQL driver
Step 2: Create connection properties
Connection
object:<code class="java">DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name", "username", "password");</code>
Replace the following properties with your own MySQL database information:
localhost
: Database server hostname or IP address3306
: MySQL default port database_name
: The name of the database to be connected username
: MySQL user Namepassword
: MySQL password##Step 3: Execute query
object to execute the query:
<code class="java">Statement statement = connection.createStatement();</code>
<code class="java">String query = "SELECT * FROM table_name"; ResultSet resultSet = statement.executeQuery(query);</code>
To get query results:
<code class="java">while (resultSet.next()) { // 获取列值并处理 }</code>
Sample code
<code class="java">import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Main { public static void main(String[] args) { try { // 添加 MySQL 驱动器 Class.forName("com.mysql.cj.jdbc.Driver"); // 创建连接 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name", "username", "password"); // 执行查询 Statement statement = connection.createStatement(); String query = "SELECT * FROM table_name"; ResultSet resultSet = statement.executeQuery(query); // 解析查询结果 while (resultSet.next()) { System.out.println("ID: " + resultSet.getInt("id")); } // 关闭资源 resultSet.close(); statement.close(); connection.close(); } catch (SQLException | ClassNotFoundException e) { e.printStackTrace(); } } }</code>
The above is the detailed content of How to connect navicat to javaweb. For more information, please follow other related articles on the PHP Chinese website!