工具类:
Code
1import java.sql.Connection;
2import java.sql.DriverManager;
3import java.sql.ResultSet;
4import java.sql.SQLException;
5import java.sql.Statement;
6
7
8public final class JDBCUtils {
9
10 private JDBCUtils(){}
11
12 private static String url="jdbc:mysql://localhost:3306/forum";
13 private static String user="root";
14 private static String password="root";
15
16 static{
17 try {
18 Class.forName("com.mysql.jdbc.Driver");
19 } catch (ClassNotFoundException e) {
20 throw new ExceptionInInitializerError(e);
21 }
22 }
23
24 public static Connection getConnection() throws SQLException
25 {
26 return DriverManager.getConnection(url, user, password);
27 }
28
29 public static void free(ResultSet rs,Statement st,Connection conn)
30 {
31 try{
32
33 if(rs!=null)
34 rs.close();
35 }catch(SQLException e){
36 e.printStackTrace();
37 }finally{
38
39 try{
40 if(st!=null)
41 st.close();
42 }catch(SQLException e){
43 e.printStackTrace();
44 }finally{
45
46 try{
47 if(conn!=null)
48 conn.close();
49 }catch(SQLException e){
50 e.printStackTrace();
51 }
52 }
53 }
54 }
55}
56
1import java.sql.Connection;
2import java.sql.DriverManager;
3import java.sql.ResultSet;
4import java.sql.SQLException;
5import java.sql.Statement;
6
7
8public final class JDBCUtils {
9
10 private JDBCUtils(){}
11
12 private static String url="jdbc:mysql://localhost:3306/forum";
13 private static String user="root";
14 private static String password="root";
15
16 static{
17 try {
18 Class.forName("com.mysql.jdbc.Driver");
19 } catch (ClassNotFoundException e) {
20 throw new ExceptionInInitializerError(e);
21 }
22 }
23
24 public static Connection getConnection() throws SQLException
25 {
26 return DriverManager.getConnection(url, user, password);
27 }
28
29 public static void free(ResultSet rs,Statement st,Connection conn)
30 {
31 try{
32
33 if(rs!=null)
34 rs.close();
35 }catch(SQLException e){
36 e.printStackTrace();
37 }finally{
38
39 try{
40 if(st!=null)
41 st.close();
42 }catch(SQLException e){
43 e.printStackTrace();
44 }finally{
45
46 try{
47 if(conn!=null)
48 conn.close();
49 }catch(SQLException e){
50 e.printStackTrace();
51 }
52 }
53 }
54 }
55}
56
使用的例子:
Code
1public static void main(String[] args) {
2 // TODO Auto-generated method stub
3 try {
4 JDBCUtilsTest();
5 } catch (Exception e) {
6 // TODO Auto-generated catch block
7 e.printStackTrace();
8 }
9
10 }
11
12
13 public static void JDBCUtilsTest() throws Exception
14 {
15 Connection conn=null;
16 Statement st=null;
17 ResultSet rs=null;
18
19 try{
20 conn=JDBCUtils.getConnection();
21 st=conn.createStatement();
22 rs=st.executeQuery("select * from user");
23 while(rs.next())
24 {
25 System.out.println(rs.getObject(1)+"\t"+rs.getObject(2));
26 }
27 }finally{
28 JDBCUtils.free(rs, st, conn);
29 }
30
31 }
1public static void main(String[] args) {
2 // TODO Auto-generated method stub
3 try {
4 JDBCUtilsTest();
5 } catch (Exception e) {
6 // TODO Auto-generated catch block
7 e.printStackTrace();
8 }
9
10 }
11
12
13 public static void JDBCUtilsTest() throws Exception
14 {
15 Connection conn=null;
16 Statement st=null;
17 ResultSet rs=null;
18
19 try{
20 conn=JDBCUtils.getConnection();
21 st=conn.createStatement();
22 rs=st.executeQuery("select * from user");
23 while(rs.next())
24 {
25 System.out.println(rs.getObject(1)+"\t"+rs.getObject(2));
26 }
27 }finally{
28 JDBCUtils.free(rs, st, conn);
29 }
30
31 }