Java link MySQL

Source: Internet
Author: User

1. Basic parameters

// JDBC driver name and database URL    Static Final String jdbc_driver = "Com.mysql.jdbc.Driver";       // localhost: local    Static Final String Db_url = "Jdbc:mysql://localhost:3306/mysql";      // The user name and password of the database need to be set    according to their own Static Final String USER = "root";     Static Final String PASS = "123456";

2. Inject drive connection database, Zzz_form table for name retrieval, close database

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 name from Zzz_form"; ResultSet RS=stmt.executequery (SQL); //Expand the result set database             while(Rs.next ()) {//Retrieving by FieldsString name = rs.getstring ("name");                Mynamelist.add (name);            SYSTEM.OUT.PRINTLN (name); }            //Close after completionRs.close ();            Stmt.close ();        Conn.close (); }Catch(SQLException se) {//Handling JDBC ErrorsSystem.out.print ("Handling JDBC errors");        Se.printstacktrace (); }Catch(Exception e) {//Handling Class.forName ErrorsSystem.out.print ("Handling Class.forName Error");        E.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 (); }        }

Description

1. Import library Mysql-connector-java-5.1.42-bin.jar

Java link MySQL

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.