Java programs connect to MySQL database

Source: Internet
Author: User
Tags stmt

Driver: Mysql-connector-java-5.1.7-bin.jar.

Examples of the program are:

 PackageCommonproject;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement; Public classJAVAFORJDBC { Public Static voidMain (string[] args)throwsSQLException, classnotfoundexception {String dbdriver= "Com.mysql.jdbc.Driver";//declaring MySQL driverString url = "JDBC:MYSQL://139.129.34.134:3306/DATABASE01";//declaring a data sourceConnection conn =NULL;//declaring a connection to a databaseStatement stmt =NULL;//declaring execution SQL statementsResultSet rs =NULL;//declaring a result setClass.forName (dbdriver);//load the database driver, generally can not execute, the execution of this sentence can be checked in advance whether the JDBC driver exists. conn = drivermanager.getconnection (URL, "username", "password");//connecting to a databasestmt = Conn.createstatement ();//Create a statement objectrs = Stmt.executequery ("SELECT * from Usertable"); Execute Query Statement while(Rs.next ()) {System.out.println ("ID:" +rs.getstring (1) + "Name:" +rs.getstring (2) + "Age:" +rs.getstring (3)); }    }}

Java programs connect to MySQL database

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.