JAVA版的SqlHelper【自学jdbc3个晚上的总结】
自学了3个晚上的jdbc,感觉还比较陌生,比如事务,并发那块还没有去学习,这里就简单山寨了个java班的SqlHelper
大家看了代码后,尽管血喷。
jdbc操作流程:
1、下载相关数据库的jdbc驱动,放在硬盘某个目录中
2、配置classpath环境变量,把下载的jdbc驱动里面的jar路径添加到classpath中
3、在代码中调用Class.forName方法加载驱动,参数为jar里面的类全名称
4、调用DriverManager.getConnection方法,参数为连接字符串
5、创建各种Statement,执行sql
个人感觉,jdbc好麻烦,呵呵,相对于C#而言,比如:操作流程、批量sql操作、参数化sql操作等等,都很麻烦。
View Code
1 package lbc;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.PreparedStatement;
6 import java.sql.ResultSet;
7 import java.sql.ResultSetMetaData;
8 import java.sql.SQLException;
9 import java.sql.Statement;
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14
15 public final class SqlHelper {
16
17 private SqlHelper() {
18
19 }
20
21 static {
22 //加载驱动
23 try {
24 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
25 } catch (ClassNotFoundException e) {
26 e.printStackTrace();
27 throw new RuntimeException(e);
28 }
29 }
30
31 private static Connection getConnection(String connectionString) throws SQLException {
32 Connection connection = DriverManager.getConnection(connectionString);
33 return connection;
34 }
35
36 private static void close(Connection connection,Statement statement) {
37 try {
38 if(connection != null){
39 connection.close();
40 }
41 if (statement != null) {
42 statement.close();
43 }
44 } catch (Exception e) {
45 e.printStackTrace();
46 }
47 }
48
49 private static void close(ResultSet rs){
50 try {
51 if (rs != null) {
52 rs.close();
53 }
54 } catch (Exception e) {
55 e.printStackTrace();
56 }
57 }
58
59 private static List<Map<String, Object>> convert(ResultSet rs) throws SQLException {
60 List<Map<String, Object>> list = new ArrayList<>();
61 if(rs != null && !rs.isClosed()){
62 ResultSetMetaData meta = rs.getMetaData();
63 int colCount=meta.getColumnCount();
64 while (rs.next()) {
65 Map<String, Object> map = new HashMap<String, Object>();
66 for(int i=1;i<=colCount;i++){
67 String key = meta.getColumnName(i);
68 Object value = rs.getObject(i);
69 map.put(key, value);
70 }
71 list.add(map);
72
73 }
74 }
75 return list;
76 }
77
78 private static void AttachParameters(PreparedStatement statement,Object ...values) throws Exception {
79 if (values != null) {
80 for (int i = 0; i < values.length; i++) {
81 statement.setObject(i + 1, values[i]);
82 }
83 }
84 }
85
86 public static int ExecuteNonQuery(String sql,String connectionString,Object ...parameterValues) throws SQLException,Exception {
87 Connection connection = null;
88 PreparedStatement statement = null;
89 int result = 0;
90 try {
91 connection = getConnection(connectionString);
92 statement = connection.prepareStatement(sql);
93 AttachParameters(statement, parameterValues);
94 result = statement.executeUpdate();
95 statement.clearParameters();
96 }
97 finally {
98 close(connection, statement);
99 }
100 return result;
101 }
102
103 public static Object ExecuteScalar(String sql,String connectionString,Object ...parameterValues) throws Exception {
104 ResultSet rs = null;
105 Object value = null;
106 try {
107 rs = ExecuteResultSet(sql,connectionString,parameterValues);
108 if(rs != null){
109 rs.next();
110 value = rs.getObject(1);
111 }
112 }
113 finally {
114 close(rs);
115 }
116
117 return value;
118 }
119
120 public static ResultSet ExecuteResultSet(String sql,String connectionString,Object ...parameterValues) throws Exception {
121 Connection connection = null;
122 PreparedStatement statement = null;
123 ResultSet rs = null;
124 try {
125 connection = getConnection(connectionString);
126 statement = connection.prepareStatement(sql);
127 AttachParameters(statement, parameterValues);
128 rs = statement.executeQuery();
129 statement.clearParameters();
130 }
131 catch (Exception e) {
132 close(connection,statement);
133 throw e;
134 }
135 return rs;
136 }
137
138 public static List<Map<String, Object>> ExecuteList(String sql,String connectionString,Object ...parameterValues) throws Exception {
139 ResultSet rs = null;
140 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
141 try {
142 rs = ExecuteResultSet(sql, connectionString,parameterValues);
143 list = convert(rs);
144 }
145 finally {
146 close(rs);
147 }
148 return list;
149 }
150
151
152 }
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.PreparedStatement;
6 import java.sql.ResultSet;
7 import java.sql.ResultSetMetaData;
8 import java.sql.SQLException;
9 import java.sql.Statement;
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14
15 public final class SqlHelper {
16
17 private SqlHelper() {
18
19 }
20
21 static {
22 //加载驱动
23 try {
24 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
25 } catch (ClassNotFoundException e) {
26 e.printStackTrace();
27 throw new RuntimeException(e);
28 }
29 }
30
31 private static Connection getConnection(String connectionString) throws SQLException {
32 Connection connection = DriverManager.getConnection(connectionString);
33 return connection;
34 }
35
36 private static void close(Connection connection,Statement statement) {
37 try {
38 if(connection != null){
39 connection.close();
40 }
41 if (statement != null) {
42 statement.close();
43 }
44 } catch (Exception e) {
45 e.printStackTrace();
46 }
47 }
48
49 private static void close(ResultSet rs){
50 try {
51 if (rs != null) {
52 rs.close();
53 }
54 } catch (Exception e) {
55 e.printStackTrace();
56 }
57 }
58
59 private static List<Map<String, Object>> convert(ResultSet rs) throws SQLException {
60 List<Map<String, Object>> list = new ArrayList<>();
61 if(rs != null && !rs.isClosed()){
62 ResultSetMetaData meta = rs.getMetaData();
63 int colCount=meta.getColumnCount();
64 while (rs.next()) {
65 Map<String, Object> map = new HashMap<String, Object>();
66 for(int i=1;i<=colCount;i++){
67 String key = meta.getColumnName(i);
68 Object value = rs.getObject(i);
69 map.put(key, value);
70 }
71 list.add(map);
72
73 }
74 }
75 return list;
76 }
77
78 private static void AttachParameters(PreparedStatement statement,Object ...values) throws Exception {
79 if (values != null) {
80 for (int i = 0; i < values.length; i++) {
81 statement.setObject(i + 1, values[i]);
82 }
83 }
84 }
85
86 public static int ExecuteNonQuery(String sql,String connectionString,Object ...parameterValues) throws SQLException,Exception {
87 Connection connection = null;
88 PreparedStatement statement = null;
89 int result = 0;
90 try {
91 connection = getConnection(connectionString);
92 statement = connection.prepareStatement(sql);
93 AttachParameters(statement, parameterValues);
94 result = statement.executeUpdate();
95 statement.clearParameters();
96 }
97 finally {
98 close(connection, statement);
99 }
100 return result;
101 }
102
103 public static Object ExecuteScalar(String sql,String connectionString,Object ...parameterValues) throws Exception {
104 ResultSet rs = null;
105 Object value = null;
106 try {
107 rs = ExecuteResultSet(sql,connectionString,parameterValues);
108 if(rs != null){
109 rs.next();
110 value = rs.getObject(1);
111 }
112 }
113 finally {
114 close(rs);
115 }
116
117 return value;
118 }
119
120 public static ResultSet ExecuteResultSet(String sql,String connectionString,Object ...parameterValues) throws Exception {
121 Connection connection = null;
122 PreparedStatement statement = null;
123 ResultSet rs = null;
124 try {
125 connection = getConnection(connectionString);
126 statement = connection.prepareStatement(sql);
127 AttachParameters(statement, parameterValues);
128 rs = statement.executeQuery();
129 statement.clearParameters();
130 }
131 catch (Exception e) {
132 close(connection,statement);
133 throw e;
134 }
135 return rs;
136 }
137
138 public static List<Map<String, Object>> ExecuteList(String sql,String connectionString,Object ...parameterValues) throws Exception {
139 ResultSet rs = null;
140 List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
141 try {
142 rs = ExecuteResultSet(sql, connectionString,parameterValues);
143 list = convert(rs);
144 }
145 finally {
146 close(rs);
147 }
148 return list;
149 }
150
151
152 }