Java link MySQL

Source: Internet
Author: User

1. First install MySQL, add the bin directory to the PATH environment variable

2. Modify the SQL login name to 123456.update mysql.user set Authentication_string=password (' 123456 ') where user= ' root '

3.mysql download mysql-connector-java-5.1.44, project properties build path->configure build path->libraries->add external jars Add Add mysql-connector-java-5.1.44 folder inside the Mysql-connector-java-5.1.44-bin.jar component

4. Open MySQL service login, show database, add table

net start MySQL

mysql-uroot-p123456

Show databases

Use ABC

5 Connection Codes

Import java.sql.*;

public class Maindemo {
public static void Main (string[] args) {
try{
Call the Class.forName () method to load the driver
Class.forName ("Com.mysql.jdbc.Driver");
SYSTEM.OUT.PRINTLN ("Load MySQL driver successfully!") ");
}catch (ClassNotFoundException E1) {
System.out.println ("MySQL driver not found!");
E1.printstacktrace ();
}

String url= "JDBC:MYSQL://LOCALHOST:3306/ABC"; URL for JDBC
Connection Conn;
try {
conn = drivermanager.getconnection (URL, "root", "123456");
Create a statement object
Statement stmt = Conn.createstatement (); Create a statement object
System.out.print ("Successfully connected to the database! ");

String sql= "SELECT * from Abcbiao1";
ResultSet rs=stmt.executequery (SQL);
System.out.println ("number" + "\ T" + "name" + "\ T" + "age");

while (Rs.next ())
{
System.out.print (Rs.getint (1) + "\ T");
System.out.print (rs.getstring (2) + "\ T");
System.out.println ();

}

Rs.close ();
Stmt.close ();
Conn.close ();
} catch (SQLException e) {
SYSTEM.OUT.PRINTLN ("Connection not successful");
E.printstacktrace ();
}
}
}

Java link MySQL

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.