Brief introduction:
How can we connect to a database with good code written in eclipse? For beginners, especially in connection with the database this block often has some inexplicable errors, in general, the root of our connection to the database process error. So how do we solve it? Then I'll share my connection with the database operation process, as a reference bar!
First open the Navicat premium software, click on the file, then click on New Connection, select MySQL, I personally like to use the diagram to describe:
Fill in the connection name (take an English name), then fill in the password, remember your user name
Click Connect Test:
Some of them will report such errors:
Reporting this error means that the connection initialization is lost, we can change the localhost to 127.0.0.1 directly.
In the Click Connect Test:
Click OK to make sure it's all right,
So we've built the connection. So let's create a database: Right-click the built-in connection name and click New Database:
Fill in the database name and select the character set to UTF-8
Click OK, OK, after the database is built we start to create the table, expand the database, find the table, right click, click New Table:
Fill in the properties of the table: set the ID to give it a primary key, select Auto Increment, when we add content, let the ID automatically sequentially increment
Click Save:
Enter the table name, click OK,
So we've created the MySQL table:
After you have created the MySQL table, open Eclipse, click File---->new---->project
Pop up the box below to find Javaproject, then click Next
Pop-up box, take a project name, then click Finish
Such a Java project is created.
To import a jar package to the project first, if the jar is not available, eclipse cannot connect to MySQL, so we will first import the jar package as a project:
The simplest way to do this is to simply copy the package, paste it into the project, then right-click the jar package and find the build path---->add to build path
The box that pops up below indicates that the jar package has been directed
After the jar package is guided, create a class for the project.
After the class is created, the process of using code to implement Eclipse connection to the database is as follows:
Package Bw.jg.helloword;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.SQLException; public class Helloword {public static void main (string[] args) {//TODO auto-generated method Stubconnection conn = null;/ /Drive engine string JD = "Com.mysql.jdbc.Driver";//connection to MySQL string url = "Jdbc:mysql://127.0.0.1:3306/hello";// MySQL username string user = "root";//mysql password string password = "123456"; try {//Load driver Class.forName (JD);//Connect database Object conn = Driverm Anager.getconnection (URL, user, password); SYSTEM.OUT.PRINTLN ("Connection succeeded");} catch (Exception e) {//TODO auto-generated catch BlockSystem.out.println ("Engine failed"); E.printstacktrace ();} IF (conn! = null) {try {//close resource conn.close ();} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ( );}}}}
Run:
This completes the entire process of using eclipse to connect to the database.
Add:
Import this code: first open the jar package we just came in:
Open the first package, locate Driver.class, right-click, locate copy qualified Name, click
Then go back to the code and paste as:
When you're done, remove the Java and it's OK.
How eclipse connects to a table in MySQL!!!!!