Java connection MySQL database detailed steps parsing _java

Source: Internet
Author: User

The first step: download a JDBC driver package, for example, I used: Mysql-connector-java-5.1.17-bin.jar

The second step: the introduction of the download of the JDBC driver package, I used the MyEclipse, select their own to guide the package of items, right click to select Propertise, and then select Javabuild Path, right will appear libreries, point in, and then point add External JARs and then find the driver package you want to import. After the order Andexport, then select the package you imported.

Step three: load driver: Class.forName ("Com.mysql.jdbc.Driver");

Fourth Step: Connection Database: Connection conn=drivermanager.getconnection ("jdbc:mysql://localhost/database name", "Root", "123456");

Step Fifth: declare a Statement used to execute the SQL statement: Statement stmt=conn.createstatement ();

Step Sixth: Declare a result set to catch the data that executes the SQL statement: ResultSet rs=stmt.executequery ("SELECT * from table name");

The complete code is given below:

Copy Code code as follows:

try {
Class.forName ("Com.mysql.jdbc.Driver");
SYSTEM.OUT.PRINTLN ("test Pass");
Connection conn=drivermanager.getconnection ("Jdbc:mysql://localhost/myschool", "root", "123456");
SYSTEM.OUT.PRINTLN ("conn-------------" +conn);
Statement stmt=conn.createstatement ();
ResultSet rs=stmt.executequery ("SELECT * from admin");
while (Rs.next ()) {
String name=rs.getstring ("name");
String pwd=rs.getstring ("PWDs");
SYSTEM.OUT.PRINTLN ("name------" +name+ "--------pwd-" +pwd);
}
catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

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.