Java JDBC Connection MySQL Database

Source: Internet
Author: User


1 Importjava.sql.Connection;2 ImportJava.sql.DriverManager;3 ImportJava.sql.ResultSet;4 Importjava.sql.SQLException;5 Importcom.mysql.jdbc.PreparedStatement;6 7 Public classDBTool {8 9 Public Static FinalString DRIVER = "Com.mysql.jdbc.Driver" ; Ten Public Static FinalString URL = "jdbc:mysql://localhost:3306/database name"; One Public Static FinalString USERNAME = "User name" ; A Public Static FinalString PSW = "Password" ; - - Public StaticConnection Getconn () { theConnection conn =NULL; - Try { - Class.forName (DRIVER); -conn =drivermanager.getconnection (URL,USERNAME,PSW); +}Catch(ClassNotFoundException e) { - e.printstacktrace (); +}Catch(SQLException e) { A e.printstacktrace (); at } - returnConn; - } - //Close Resource - Public Static voidCloseAll (ResultSet rs, PreparedStatement ps,connection conn) { - if(rs!=NULL){ in Try { - rs.close (); to}Catch(SQLException e) { + e.printstacktrace (); - } the } * if(ps!=NULL){ $ Try {Panax Notoginseng rs.close (); -}Catch(SQLException e) { the e.printstacktrace (); + } A } the if(conn!=NULL){ + Try { - conn.close (); $}Catch(SQLException e) { $ e.printstacktrace (); - } - } the } - Wuyi}

Java JDBC Connection 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.