码迷,mamicode.com
首页 > 数据库 > 详细

JDBC--01

时间:2017-10-02 00:52:09      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:查找   new   auto   tac   驱动   mysq   port   sele   编码   

jdbc(mysql)配置文件
文件名:database.properties
以下为配置文件里所写的信息:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/6521?useUnicode=true&characterEncoding=utf8
user=root
password=root
?useUnicode=true&characterEncoding=utf8此为设置编码。注意mysql设置为utf-8时写的是utf8。
  1 JDBC加载工具类方式一:
  2 package com.it.JDBC;
  3 import java.sql.Connection;
  4 import java.sql.DriverManager;
  5 import java.sql.PreparedStatement;
  6 import java.sql.ResultSet;
  7 import java.sql.SQLException;
  8 import java.util.ResourceBundle;
  9 
 10 public class JDBCUtils_V1 {
 11     private static String driver = null;
 12     private static String url = null;
 13     private static String user = null;
 14     private static String password = null;
 15 
 16     /**
 17      * @author Payphone
 18      * @time 2017-10-01 加载配置信息
 19      * */
 20     static {
 21         ResourceBundle bundle = ResourceBundle.getBundle("database");
 22         driver = bundle.getString("driver");
 23         url = bundle.getString("url");
 24         user = bundle.getString("user");
 25         password = bundle.getString("password");
 26     }
 27 
 28     /**
 29      * @time 2017-10-01
 30      * @author Payphone连接数据库
 31      * */
 32     public static Connection getConnection() {
 33         Connection conn = null;
 34         try {
 35             // 1 加载驱动
 36             Class.forName(driver);
 37             // 2获得连接
 38             conn = DriverManager.getConnection(url, user, password);
 39             if (conn != null) {
 40                 System.out.println("连接成功");
 41             } else {
 42                 System.out.println("失败了");
 43             }
 44         } catch (Exception e) {
 45             e.printStackTrace();
 46         }
 47         return conn;
 48     }
 49 
 50     /**
 51      * @author Payphone释放资源
 52      * @time 2017-10-01
 53      * */
 54     public static void Release(Connection conn, PreparedStatement psmt,
 55             ResultSet rs) {
 56         try {
 57             if (rs != null) {
 58                 conn.close();
 59             }
 60             if (psmt != null) {
 61                 psmt.close();
 62             }
 63             if (conn != null) {
 64                 conn.close();
 65             }
 66             System.out.println("释放资源成功");
 67         } catch (SQLException e) {
 68             e.printStackTrace();
 69         }
 70 
 71     }
 72 }
 73 
 74 JDBC加载工具类方式二:
 75  package com.it.JDBC;
 76 import java.io.IOException;
 77 import java.io.InputStream;
 78 import java.sql.Connection;
 79 import java.sql.DriverManager;
 80 import java.sql.PreparedStatement;
 81 import java.sql.ResultSet;
 82 import java.sql.SQLException;
 83 import java.util.Properties;
 84 import org.junit.Test;
 85 
 86 public class JDBCUtils_V2 {
 87     private static String driver = null;
 88     private static String url = null;
 89     private static String user = null;
 90     private static String password = null;
 91 
 92     /**
 93      * @author Payphone
 94      * @time 2017-10-01 加载配置信息
 95      * */
 96     static {
 97         try {
 98             // 1.通过当前类获取类加载器
 99             ClassLoader classLoader = JDBCUtils_V2.class.getClassLoader();
100             // 2.通过类加载器的方法获得一个输入流
101             InputStream is = classLoader
102                     .getResourceAsStream("database.properties");
103             // 3.创建一个properties对象
104             Properties props = new Properties();
105             // 4.加载输入流
106             props.load(is);
107             // 5.获取相关参数的值
108             driver = props.getProperty("driver");
109             url = props.getProperty("url");
110             user = props.getProperty("user");
111             password = props.getProperty("password");
112         } catch (IOException e) {
113             // TODO Auto-generated catch block
114             e.printStackTrace();
115         }
116         
117 
118     }
119 
120     /**
121      * @time 2017-10-01
122      * @author Payphone连接数据库
123      * */
124     @Test
125     public static Connection getConnection() {
126         Connection conn = null;
127         try {
128             // 1 加载驱动
129             Class.forName(driver);
130             // 2获得连接
131             conn = DriverManager.getConnection(url, user, password);
132             if (conn != null) {
133                 System.out.println("连接成功");
134             } else {
135                 System.out.println("失败了");
136             }
137         } catch (Exception e) {
138             e.printStackTrace();
139         }
140         return conn;
141     }
142 
143     /**
144      * @author Payphone释放资源
145      * @time 2017-10-01
146      * */
147     public static void Release(Connection conn, PreparedStatement psmt,
148             ResultSet rs) {
149         try {
150             if (rs != null) {
151                 conn.close();
152             }
153             if (psmt != null) {
154                 psmt.close();
155             }
156             if (conn != null) {
157                 conn.close();
158             }
159             System.out.println("释放资源成功");
160         } catch (SQLException e) {
161             e.printStackTrace();
162         }
163 
164     }
165 }
166 
167 JDCB测试类(测试用的junit):
168 package com.it.JDBC;
169 import java.sql.Connection;
170 import java.sql.PreparedStatement;
171 import java.sql.ResultSet;
172 import java.sql.SQLException;
173 import org.junit.Test;
174 
175 /**
176  * @author Payphone
177  * @version 1.0
178  * @描述 数据库操作工具类,主要进行增删改查
179  */
180 public class JDBCUtilsTest {
181     Connection conn = null;
182     PreparedStatement psmt = null;
183     ResultSet rs = null;
184     String sql = null;
185 
186     /**
187      * @author Payphone
188      * @version 1.0
189      * @描述 根据id查找用户信息
190      */
191     @Test
192     public void findById() {
193         try {
194             // 1 获取连接
195             conn = JDBCUtils_V2.getConnection();
196             sql = "select * from t_user where id=?";
197             psmt = conn.prepareStatement(sql);
198             psmt.setInt(1, 2015008);
199             rs = psmt.executeQuery();
200             while (rs.next()) {
201                 System.out.println(rs.getString("id") + ":"
202                         + rs.getString("pwd"));
203             }
204         } catch (SQLException e) {
205             // TODO Auto-generated catch block
206             e.printStackTrace();
207         } finally {
208             JDBCUtils_V2.Release(conn, psmt, rs);
209         }
210     }
211 
212     /**
213      * 插入用户信息
214      */
215     @Test
216     public void insertMessage() {
217         try {
218             conn = JDBCUtils_V1.getConnection();
219             sql = "insert into t_user(id,pwd) values(?,?)";
220             PreparedStatement psmt = conn.prepareStatement(sql);
221             psmt.setInt(1, 2015002);
222             psmt.setString(2, "2015002");
223             int rs = psmt.executeUpdate();
224             if (rs > 0) {
225                 System.out.println("插入成功!");
226             }
227         } catch (SQLException e) {
228             System.out.println("插入失败,数据重复了!");
229             e.printStackTrace();
230         } finally {
231             JDBCUtils_V1.Release(conn, psmt, null);
232         }
233     }
234 
235     /**
236      * 更新用户的信息
237      * */
238     @Test
239     public void updateMessage() {
240         try {
241             conn = JDBCUtils_V1.getConnection();
242             sql = "update t_user set pwd=? where id=?";
243             psmt = conn.prepareStatement(sql);
244             psmt.setString(1, "LH");
245             psmt.setInt(2, 2015002);
246             int r = psmt.executeUpdate();
247             if (r > 0) {
248                 System.out.println("密码修改成功" + ":");
249             }
250         } catch (SQLException e) {
251             System.out.println("密码修改失败!");
252             e.printStackTrace();
253         } finally {
254             JDBCUtils_V1.Release(conn, psmt, null);
255         }
256     }
257 
258     /**
259      * 根据用户id删除信息
260      * */
261     @Test
262     public void deleteById() {
263         try {
264             conn = JDBCUtils_V1.getConnection();
265             sql = "delete from t_user where id=?";
266             psmt = conn.prepareStatement(sql);
267             psmt.setInt(1, 2015002);
268             int r = psmt.executeUpdate();
269             if(r>0){
270                 System.out.println("删除成功");
271             }
272         } catch (SQLException e) {
273             System.out.println("删除失败");
274             e.printStackTrace();
275         } finally {
276             JDBCUtils_V1.Release(conn, psmt, null);
277         }
278     }
279 }

第二篇JDBC的将推送JDBC连接池的。

 

JDBC--01

标签:查找   new   auto   tac   驱动   mysq   port   sele   编码   

原文地址:http://www.cnblogs.com/ljwdemo/p/7618329.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!