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.
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 finish7. 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(); } } }
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!