Java link jdbc

Source: Internet
Author: User

1 Importjava.sql.Connection;2 ImportJava.sql.DriverManager;3 ImportJava.sql.ResultSet;4 Importjava.sql.SQLException;5 Importjava.sql.Statement;6 ImportJava.util.ResourceBundle;7 8  Public classConnnectionutils {9 Ten     Private StaticString dbdriver = "";//Com.mysql.jdbc.Driver One     Private StaticString url = "";//jdbc\:mysql\://Localhost\:3306/dabasename?useunicode\=true&characterencoding\=utf-8 A     Private StaticString user = "";//username -     Private StaticString PASSW = "";//Password -      the     StaticConnection conn=NULL; -      -     Static { -          +ResourceBundle Source = resourcebundle.getbundle ("config");//config.properties -Dbdriver = source.getstring ("Driverclassname"); +url = source.getstring ("Jdbc_url"); Auser = Source.getstring ("Jdbc_username"); atPASSW = source.getstring ("Jdbc_password"); -     } -      -      Public StaticConnection getconnection () { -         Try { -             //Load Driver in Class.forName (dbdriver); -             //Get links toconn =drivermanager.getconnection (URL,USER,PASSW); +}Catch(Exception e) { - e.printstacktrace (); the         } *          $         returnConn;Panax Notoginseng     } -      the     //close link after use +      Public Static voidClose () { A         if(Conn! =NULL) { the             Try { + conn.close (); -}Catch(SQLException e) { $ e.printstacktrace (); $             } -         } -     } the      -     Wuyi      the      -      Public Static voidMain (string[] args) { WuConnection conn =connnectionutils.getconnection (); - SYSTEM.OUT.PRINTLN (conn); AboutString sql = "SELECT * FROM TableName"; $          -         Try { -             //Execute SQL -Statement Statement =conn.createstatement (); A             //get the result set +ResultSet result =statement.executequery (SQL); the              -             //If there is a result set, the result is processed $              while(Result.next ()) { theString App_url = result.getstring ("Onestringcolumnname"); the System.out.println (app_url); the                  the             } -             //Close Link in connnectionutils.close (); the}Catch(SQLException e) { the e.printstacktrace (); About         } the          the     } the}

qq:871820604

Java link jdbc

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.