MySQL Getting Started JDBC

Source: Internet
Author: User

In the Java program to connect MySQL, first to the MySQL website to download the driver, and install, the default installation on the C drive (my is the default installation, the directory is C:\Program files\mysql\mysql Connector J), If MySQL is also the default installation, the path you just installed is below the MySQL directory, the address is http://www.mysql.com/products/connector/, and if it is Java, download the JDBC Driver for MySQL ( CONNECTOR/J)

After the download completes the installation, the Mysql-connector-java-5.1.30-bin.jar is introduced in the development tool after the installation, and the following code is introduced:

Package Com.jll.model;

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import java.util.LinkedList;
Import java.util.List;

public class Driver {
public static void Main (string[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
List List = new LinkedList ();
try {
Class.forName ("Com.mysql.jdbc.Driver");
conn = Drivermanager.getconnection ("Jdbc:mysql://localhost/mydata?" +
"User=root&password=root");
stmt = Conn.createstatement ();
rs = Stmt.executequery ("SELECT * from article");
} catch (ClassNotFoundException e) {
E.printstacktrace ();
} catch (SQLException e) {
E.printstacktrace ();
}

try {
while (Rs.next ()) {
int i = rs.getint ("id");
List.add (i);
}
} catch (SQLException e) {
E.printstacktrace ();
}finally{
try {
Rs.close ();
Stmt.close ();
Conn.close ();
} catch (SQLException e) {
E.printstacktrace ();
}
}

for (int i = 0;i<list.size (); i++) {
System.out.println (List.get (i));
}
}

}

Related Article

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.