Home > Java > javaTutorial > How to connect eclipse to mysql

How to connect eclipse to mysql

(*-*)浩
Release: 2020-09-15 14:19:36
Original
19031 people have browsed it

Connection steps: 1. Create a project (My); 2. Right-click My and select "build Path"->"add external jars.."; 3. Find the jdbc storage directory and select "MySQL.jar", click "Open"; 4. Click apply and apply and close.

How to connect eclipse to mysql

Preparation work: Download MySQL and jdbc, which I won’t go into here. Note that you may have downloaded MySQL, but not jdbc. Their downloads are separate, so you can first look at some of the steps at the end of this article to make sure you have downloaded jdbc. If you have not downloaded it, go to the official website to download it. It is only a few megabytes and very fast.

1. First open eclipse and create a new project "My". The specific operation is:

Click "File"----Click "new"----Click "java project" , get the page as shown below, fill in the project name in the project name, such as "My", and then click "finish"

2. At this time, on the left we can see that the project has been Created

Right-click "My"-"build path"-"configure build path"

Click the second "add external jars" in the zip code here in libraries, find the location where you downloaded jdbc and decompressed it, find the MySQL jar and open it

3. Click apply and apply and close

##4. Click My and click rreferenced libraries. The interface is as follows. Just see the jar package

5. Open the MySQL client, enter your own password, show databases; check which databases you have

6. Next use the program Test src - right click new - class - fill in name: jdbc, check the first one at the bottom, and then finish

7. The code is as follows

import java.sql.*;
public class Jdbc {
  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/test","root","java");//java这个空填写的是你自己设的密码
           
      System.out.println("Success connect Mysql server!");
      Statement stmt = connect.createStatement();
      ResultSet rs = stmt.executeQuery("select * from user");
                  //user 为你表的名称,可以在MySQL命令行用show tables;显示
while (rs.next()) {
        System.out.println(rs.getString("name"));
      }
    }
    catch (Exception e) {
      System.out.print("get data error!");
      e.printStackTrace();
    }
  }
}
Copy after login

8. Normally this will not go wrong, but it may go wrong due to version issues

If this happens Error, then: (1)

(2) "jdbc:mysql://localhost:3306/test?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true","root"," java");//Fill in the java field with the password you set yourself. Just change these two places. I hope it will be helpful to everyone!

The above is the detailed content of How to connect eclipse to mysql. 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