Java connection MySQL

Source: Internet
Author: User
Tags stmt

Java Connection MySQL needs a driver package, the latest version is:http://dev.mysql.com/downloads/connector/j/, extract the jar library file, and then import the library file in the corresponding project.

Download steps:

Link: Http://pan.baidu.com/s/1o8PhY2Y Password: a1g1

Example of connection code:

 Packagecom.josft.test;ImportJava.sql.*;  Public classMysqldemo {//JDBC driver name and database URL    Static FinalString jdbc_driver = "Com.mysql.jdbc.Driver"; Static FinalString Db_url = "Jdbc:mysql://localhost:3306/jsoft"; //The user name and password of the database need to be set according to their own    Static FinalString USER = "root"; Static FinalString PASS = "123456";  Public Static voidMain (string[] args) {Connection conn=NULL; Statement stmt=NULL; Try{            //registering the JDBC driverClass.forName ("Com.mysql.jdbc.Driver"); //Open LinkSYSTEM.OUT.PRINTLN ("Connect database ..."); Conn=drivermanager.getconnection (Db_url,user,pass); //Execute QuerySYSTEM.OUT.PRINTLN ("instantiating statement to ..."); stmt=conn.createstatement ();            String SQL; SQL= "SELECT id, name, url from websites"; ResultSet RS=stmt.executequery (SQL); //Expand the result set database             while(Rs.next ()) {//Retrieving by Fields                intid = rs.getint ("id"); String name= rs.getstring ("name"); String URL= rs.getstring ("url"); //Output DataSystem.out.print ("ID:" +ID); System.out.print (", Site name:" +name); System.out.print (", Site URL:" +URL); System.out.print ("\ n"); }            //Close after completionRs.close ();            Stmt.close ();        Conn.close (); }Catch(SQLException se) {//Handling JDBC ErrorsSe.printstacktrace (); }Catch(Exception e) {//Handling Class.forName ErrorsE.printstacktrace (); }finally{            //Close Resource            Try{                if(stmt!=NULL) Stmt.close (); }Catch(SQLException se2) {}//don't do anything.            Try{                if(conn!=NULL) Conn.close (); }Catch(SQLException se) {se.printstacktrace (); }} System.out.println ("Goodbye!"); }}

Reference: http://www.runoob.com/java/java-mysql-connect.html

Java connection MySQL

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.