MySQL and Java (class notes)

Source: Internet
Author: User

MYSQL Database management Tools

JAVA programming language

Database driver (Java and MySQL docking mode)

To the official web download driver load driver

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

public class sql{
public static void Main (string[] args) {
try{
Load drivers loaded into the JVM
Class.forName ("Com.mysql.jdbc.Driver");
Getconnection () method, linking to the MySQL database
Connection con = drivermanager.getconnection ("jdbc:mysql://localhost:3306/Library name", "username", "password");
Verify that the link is successful
if (!con.isclosed ())
SYSTEM.OUT.PRINTLN ("Succeeded connecting to the Databases");
Create a statement object to execute the SQL statement
Statement sta = con.createstatement ();
The SQL statement to execute
String sql = "SELECT * from student";
ResultSet class holds the obtained result set
ResultSet rs = sta.executequery (SQL);
while (Rs.next ()) {
String name = rs.getstring ("sname");
int id = rs.getint ("id");
System.out.println (name+ "\ t" +id);
}
}catch (Exception e) {
E.printstacktrace ();
}
}
}

MySQL and Java (class notes)

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.