Java Web Learning JDBC Connection __java

Source: Internet
Author: User
Tags java web
"How to establish a JDBC connection"
First to the official website mysql.com download Connector-j and then unzip it out, there is a type for the jar of the file to establish the connection to the project, there is a Lib folder, it can be.




Next, create a class and enter the following variables:
String sql = "SELECT * from Tbl_user"; For querying information
Connection Conn=null; The current database connection
Statement St =null; Used to send statements to a database
ResultSet rs = null; Result set that encapsulates the data that is queried from the database
Then press Ctrl+shift+o to select the imported class in the pop-up window: java.sql.Statement Click Next, select Java.sql.Connection click Finish;
Import the related classes:
Import java.sql.Connection;
Import Java.sql.ResultSet;
Import java.sql.Statement;


Import Java.sql.DriverManager;


try {
Class.forName ("Com.mysql.jdbc.Driver"); Registers MySQL JDBC driver, forname method initializes the class specified by the parameter, and creates a corresponding sample object, in which the string in the parameter is the JDBC driver in MySQL
Conn=drivermanager.getconnection ("jdbc:mysql://localhost:3306/jsp_db", "Root", ""); Get MySQL database connection, the first parameter is the MySQL URL includes the database hostname port number and database name, the second parameter is MySQL username, the third parameter is the MySQL password

st = Conn.createstatement (); To create an statement object
Rs=st.executequery (SQL); Send MySQL statement, return result set

while (Rs.next ())//For traversing the result set
{
System.out.print (Rs.getint ("id") + ""); Each get method is used to obtain different types of values in the database
System.out.print (rs.getstring ("name") + "");
System.out.print (rs.getstring ("password") + "");
System.out.print (rs.getstring ("email") + "");
System.out.println ();
}

catch (Exception e) {
E.printstacktrace ();

}finally{//resource cleanup, close individual data items in a certain order

try {
Rs.close (); From small to large in order to close
catch (Exception E2) {
}

try {
St.close ();
catch (Exception E2) {
}

try {
Conn.close ();
catch (Exception E2) {
}
}




When you are finished writing, click the right button and run as Java application
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.