Database helper Classes

Source: Internet
Author: User

Package com.sec.util;

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;

/**
* Database Helper Classes
*
* @author Administrator
*
*/
public class Conndb {
public static string URL = "jdbc:sqlserver://localhost:1434;databasename=shoppingdb";//Connection string
public static String USER = "sa";//Login name
public static String PWD = "123456";//Password

When the class loads, load the database driver (once this is loaded, it will not need to be loaded again)
static {
try {
Class.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
E.printstacktrace ();
}
}

/**
* Get connections
*
* @return
*/
public static Connection getconnection () {
Connection conn = null;
try {
conn = Drivermanager.getconnection (URL, USER, PWD);
Return conn;
} catch (SQLException e) {
Write log
E.printstacktrace ();
}
return null;
}

/**
* Close Related Resources
*
* @param Conn
* @param stm
* @param pstm
* @param RS
*/
public static void CloseAll (Connection conn,
PreparedStatement pstmt, ResultSet rs) {
try {
IF (conn! = null) {
Conn.close ();
}
if (pstmt! = null) {
Pstmt.close ();
}
if (rs! = null) {
Rs.close ();
}
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}

Database helper Classes

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.