JDBC Template Connection

Source: Internet
Author: User

Package itcast;

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

public class Base {

/**
* @param args
*/
public static void Main (string[] args) {

}

static void Template () throws Exception {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
2 Creating Links
Jdbcutils.getconnection ();
3 Creating statements
st = Conn.createstatement ();
4 EXECUTE statement
rs = St.executequery ("SELECT * from user");
5 Processing results
while (Rs.next ()) {
System.out.println (Rs.getobject (1) + "T" + rs.getobject (2)
+ "\ T" + rs.getobject (3) + "\ T" + rs.getobject (4)
+ "\ T" + rs.getobject (5) + "\ T");
}
} finally {
Jdbcutils.free (RS, St, conn);
}
}

}

Simple Method *********************************************************************

Package itcast;

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

Public final class Jdbcutils {
private static String URL = "Jdbc:mysql://localhost:3306/test";
private static String user = "root";
private static String password = "";

Private method of construction
Private Jdbcutils () {
}

Put the registration-driven action inside the static code block
static {
try {
Class.forName ("Com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
E.printstacktrace ();
throw new Exceptionininitializererror (e);
}
}

Establish links
public static Connection getconnection () throws SQLException {
Return drivermanager.getconnection (URL, user, password);
}

Freeing resources
public static void Free (ResultSet rs, Statement St, Connection conn) {
try {
if (rs! = null) {
Rs.close ();
}
} catch (SQLException e) {
E.printstacktrace ();
} finally {
try {
if (st! = null) {
St.close ();
}
} catch (SQLException e) {
E.printstacktrace ();
} finally {
try {
IF (conn! = null) {
Conn.close ();
}
} catch (SQLException e) {
E.printstacktrace ();
}
}
}
}
}

Single case method trouble not recommended **************************************************

Package CN.ITCAST.JDBC;

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

Public final class Jdbcutilssing {
Private String URL = "Jdbc:mysql://localhost:3306/test";
Private String user = "root";
Private String Password = "";

private static Jdbcutilssing instance = new Jdbcutilssing ();
private static jdbcutilssing instance = NULL;
Private method of construction
Private jdbcutilssing () {
}

public static jdbcutilssing getinstance () {
if (instance = = null) {
Synchronized (Jdbcutilssing.class) {
if (instance = = null) {
Instance = new Jdbcutilssing ();
}
}
}
return instance;
}

Put the registration-driven action inside the static code block
static {
try {
Class.forName ("Com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
E.printstacktrace ();
throw new Exceptionininitializererror (e);
}
}

Establish links
Public Connection getconnection () throws SQLException {
Return drivermanager.getconnection (URL, user, password);
}

Freeing resources
public void Free (ResultSet rs, Statement St, Connection conn) {
try {
if (rs! = null) {
Rs.close ();
}
} catch (SQLException e) {
E.printstacktrace ();
} finally {
try {
if (st! = null) {
St.close ();
}
} catch (SQLException e) {
E.printstacktrace ();
} finally {
try {
IF (conn! = null) {
Conn.close ();
}
} catch (SQLException e) {
E.printstacktrace ();
}
}
}
}
}

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.