How to import Mysql-connector-java-5.1.8-bin.jar "Environment Configuration and tool use" in MyEclipse

Source: Internet
Author: User

Premise: I built a Java project, project name is test, now need to connect MySQL database, So in advance from the internet to Java operation MySQL Database Mysql-connector-java-5.1.8-bin.jar package class library download down

First step: Create a Lib folder under test-src, and copy and paste the Mysql-connector-java-5.1.8-bin.jar package into the Lib directory (this is done to place the. jar package file under this project)

Second, step two: Project-"Properties (Project-)"

3, the third step: the current project under the src-"lib-" Mysql-connector-java-5.1.8-bin.jar file added to the reference, In this case we can use the class library under the Mysql-connector-java-5.1.8-bin.jar package (in fact only the path of the Mysql-connector-java-5.1.8-bin.jar package that will be introduced is referenced, i.e.: When the program runs, tell the Java virtual machine to load the Mysql-connector-java-5.1.8-bin.jar package under this path, so the jar package can be stored anywhere, we introduce just the path of the jar package. Typically we will introduce the jar package file to be placed in this project

Add the Mysql-connector-java-5.1.8-bin.jar package in the src-"src-" Lib directory to the project reference

4. Fourth step: The red part is the jar package after the successful reference

5. Fifth step: After the successful reference, the red part is the Mysql-connector-java-5.1.8-bin.jar package after the successful reference.

6. Sixth step: Remove the reference to the Mysql-connector-java-5.1.8-bin.jar package (just remove the reference to the Mysql-connector-java-5.1.8-bin.jar package from the project, and there is no actual from src-" The Mysql-connector-java-5.1.8-bin.jar package is deleted in the Lib directory, and you can then refer the Mysql-connector-java-5.1.8-bin.jar package to the project at a later time

7, set up a Testmysql.java file, test

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;

public class Testmysql {

private static String URL = "Jdbc:mysql://localhost:3306/jdbc";
private static String user = "root";
private static String password = "";


static {
try {
Class.forName ("Com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new Exceptionininitializererror (e);
}
}

public static Connection getconnection () throws SQLException {
Return drivermanager.getconnection (URL, user, password);
}

public static void Free (ResultSet rs, Statement St, Connection conn) {
try {
if (rs! = null)
Rs.close ();
} catch (SQLException e) {
E.printstacktrace ();
} finally {
try {
if (st! = NULL)
St.close ();
} catch (SQLException e) {
E.printstacktrace ();
} finally {
IF (conn! = null)
try {
Conn.close ();
} catch (SQLException e) {
E.printstacktrace ();
}
}
}
}

/**
* Select operation
* Get user information from the Gt_user table
* @throws SQLException
*/
static void Read () throws SQLException {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
2. Establish a connection
conn = getconnection ();
conn = Jdbcutilssing.getinstance (). getconnection ();
3. Create a statement
st = Conn.createstatement ();

4. Execute the statement
rs = St.executequery ("SELECT * from Gt_user limit 0,10");

5. Processing results
while (Rs.next ()) {
System.out.println (Rs.getobject ("user_id") + "\ T"
+ Rs.getobject ("username") + "\ T"
+ rs.getobject ("password") + "\ T"
+ Rs.getobject ("Realname"));
}
} finally {
Free (RS, St, conn);
}
}

/**
* @param args
* @throws SQLException
*/
public static void Main (string[] args) throws SQLException {
Read ();//Get data from a database

}

}

How to import Mysql-connector-java-5.1.8-bin.jar "Environment Configuration and tool use" in MyEclipse

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.