JDBC Connection Database

Source: Internet
Author: User

JDBC Programming steps

1: Load driver has two modes of loading drive (1) Driver driver=new com.mysql.jdbc.Driver;

(2) Class.forName ("Com.mysql.jdbc.Driver");

2: The link database corresponds to two methods of linking the database (1) Connection connection=driver.connection ();

(2) Connection connection=drivermanage.getconnection ();

3: Execute SQL statement

4: Store the contents of the database in a container

5: Access the contents of the container in turn

6: Off, last Open first close

Method One:

Import com.mysql.jdbc.Statement;
Import java.sql.Connection;
Import Java.sql.Driver;
Import Java.sql.ResultSet;
Import java.util.Properties;

/**
* Created by I am master on 2016/9/29.
*/
public class Finallyjdbc {
public static void Main (string[] args) throws exception{
Driver driver=new com.mysql.jdbc.Driver ();
String url= "Jdbc:mysql://localhost:3306/student";
Properties Properties=new properties ();//properties is a configuration file that is primarily used to express configuration information. File Format key: value
Properties.put ("User", "root");
Properties.put ("Password", "root");
Connection Connection=driver.connect (url,properties);
Statement statement= (Statement) connection.createstatement ();
String query= "SELECT * from Studentinfo";
/* Put the data in the database into a container */
ResultSet rs=statement.executequery (query);

/* Access the data in the database in turn */
while (Rs.next ()) {
System.out.print (rs.getstring ("id"));
System.out.print (rs.getstring ("name"));
System.out.print (rs.getstring ("Age"));
System.out.println (rs.getstring ("class"));
}
Rs.close ();
Statement.close ();
Connection.close ();
}
}


Method One:



JDBC Connection Database

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.