Encapsulated DBHelper (JAVA)

Source: Internet
Author: User

Jdbc+mysql

public class DBHelper {private static string driver = "Com.mysql.jdbc.Driver";p rivate static string url = "Jdbc:mysql://loc Alhost: Port number (3306)/database name "; private static string username = "Account";p rivate static string password = "password";//static, connection public static Connection Toconnection () { Connection conn = null;try {class.forname (driver); try {conn = drivermanager.getconnection (URL, username, password);} catch (SQLException e) {e.printstacktrace ();}} catch (ClassNotFoundException e) {e.printstacktrace ();} Return conn;} Close connection public static void CloseConnection (Connection conn) {if (conn!=null) {try {conn.close ()} catch (SQLException e) {// TODO auto-generated catch Blocke.printstacktrace ();}}} Close result set public static void Closeresultset (ResultSet rs) {if (rs!=null) {try {rs.close ()} catch (SQLException e) {//TODO Aut O-generated catch Blocke.printstacktrace ();} Rs=null;}} Close statementpublic static void Closepreparedstatement (PreparedStatement psmt) {if (psmt!=null) {try {psmt.close (); Psmt=null;} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} public static void Closestatement (Statement stmt) {if (stmt!=null) {try {stmt.close (); stmt=null;} catch (SQLException e) { TODO auto-generated catch Blocke.printstacktrace ();}}}

Encapsulated DBHelper (JAVA)

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.