Home Java javaTutorial How to connect eclipse to mysql

How to connect eclipse to mysql

May 11, 2019 pm 01:55 PM
eclipse

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!

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 Article Tags

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)

Why can't eclipse import the project? Why can't eclipse import the project? Jan 05, 2024 am 11:50 AM

Why can't eclipse import the project?

How to adjust background color settings in Eclipse How to adjust background color settings in Eclipse Jan 28, 2024 am 09:08 AM

How to adjust background color settings in Eclipse

Pro Guidance: Expert advice and steps on how to successfully install the Eclipse Lombok plug-in Pro Guidance: Expert advice and steps on how to successfully install the Eclipse Lombok plug-in Jan 28, 2024 am 09:15 AM

Pro Guidance: Expert advice and steps on how to successfully install the Eclipse Lombok plug-in

What are the eclipse decompilation plug-ins? What are the eclipse decompilation plug-ins? Jan 04, 2024 pm 02:21 PM

What are the eclipse decompilation plug-ins?

Revealing solutions to Eclipse code running problems: helping you troubleshoot various running errors Revealing solutions to Eclipse code running problems: helping you troubleshoot various running errors Jan 28, 2024 am 09:22 AM

Revealing solutions to Eclipse code running problems: helping you troubleshoot various running errors

Secret method and quick solution to solve Eclipse failure to start Secret method and quick solution to solve Eclipse failure to start Jan 03, 2024 pm 01:01 PM

Secret method and quick solution to solve Eclipse failure to start

How to customize shortcut key settings in Eclipse How to customize shortcut key settings in Eclipse Jan 28, 2024 am 10:01 AM

How to customize shortcut key settings in Eclipse

Step-by-step guide to changing background color with Eclipse Step-by-step guide to changing background color with Eclipse Jan 28, 2024 am 08:28 AM

Step-by-step guide to changing background color with Eclipse

See all articles