Home > Database > Mysql Tutorial > body text

Connect to mysql database using Eclipse

黄舟
Release: 2019-02-23 16:41:17
Original
14952 people have browsed it

We often use databases when developing in java. Databases can save data and manage data. Of course, connecting to the database is the first step in our development. If you don’t connect to the database, how can you Talking about operating the database, we will encounter many problems during the process of connecting to the database. Let me explain below how to connect to the database and the errors that occur during the connection process.

Recommended mysql video tutorials : "mysql tutorial"

Connect to mysql database using Eclipse

Download JDBC

1. The first step is to search "mysql-connector-java-5.1.24-bin.jar" on Baidu and download it. Put this mysql-connector-java-5.1.24-bin.jar

#Put the file into C:\jdk1.7.0_67\jre\lib\ext (this is the directory of my jdk).

Connect to mysql database using Eclipse

2. The second step (1) Open eclipse and create a java project (file-new-other-java project). I created "linkMysql1" (2) in src Create a package in - linkMysql1. Create a class in the created package - LinkMysql.

Connect to mysql database using Eclipse

##Add code


1. The third step is to write the function that loads JDBC. Note: When we test, we are all in main In the function.

try {
          Class.forName("com.mysql.jdbc.Driver");     //加载MYSQL JDBC驱动程序  
          //Class.forName("org.gjt.mm.mysql.Driver");
         System.out.println("Success loading Mysql Driver!");
        }
        catch (Exception e) {
          System.out.print("Error loading Mysql Driver!");
          e.printStackTrace();
        }
Copy after login

Connect to mysql database using Eclipse2. The fourth step is to connect to the database,

Connection connect = DriverManager.getConnection(
              "jdbc:mysql://localhost:3306/ter","root","123456");
               //连接URL为   jdbc:mysql//服务器地址/数据库名  ,后面的2个参数分别是登陆用户名和密码
Copy after login

Connect to mysql database using Eclipse3. The fifth step is to operate the database. Here my database name is ter. The ones marked in red are the familiar database operations

Connect to mysql database using Eclipse4. Step 6: Start the database service. Press CTRL+SHIFT+ESC, click Service.--Find Mysql, right-click to start

Connect to mysql database using Eclipse

##Full code

package linkMysql1;
import java.sql.*;
public class LinkMysql {
   
    public static void main(String args[]) {
        try {
          Class.forName("com.mysql.jdbc.Driver");     //加载MYSQL JDBC驱动程序  
          //Class.forName("org.gjt.mm.mysql.Driver");
         System.out.println("Success loading Mysql Driver!");
        }
        catch (Exception e) {
          System.out.print("Error loading Mysql Driver!");
          e.printStackTrace();
        }
        try {
          Connection connect = DriverManager.getConnection(
              "jdbc:mysql://localhost:3306/ter","root","123456");
               //连接URL为   jdbc:mysql//服务器地址/数据库名  ,后面的2个参数分别是登陆用户名和密码
 
          System.out.println("Success connect Mysql server!");
          Statement stmt = connect.createStatement();
          ResultSet rs = stmt.executeQuery("select * from user");
                                                                  //user 为你表的名称
    while (rs.next()) {
            System.out.println(rs.getString("name"));
          }
        }
        catch (Exception e) {
          System.out.print("get data error!");
          e.printStackTrace();
        }
      }
 
}
Copy after login

InstructionsRemember to open Mysql

The above is the detailed content of Connect to mysql database using Eclipse. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!