java常用代码分享-jdbc的数据库连接
java jdbc数据库连接
Java code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;public class JDBConnection { public Connection conn = null; // 声明Connection对象的实例 public Statement stmt = null; // 声明Statement对象的实例 public ResultSet rs = null; // 声明ResultSet对象的实例 private static String dbClassName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";//定义保存数据库驱动的变量 private static String dbUrl = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=DB_ATM"; private static String dbUser = "sa"; private static String dbPwd = "sa"; public JDBConnection(String propertyFileName) {// 带属性文件名的构造方法 Properties prop = new Properties();// 属性集合对象 InputStream is = null; try { is = JDBConnection.class.getClassLoader().getResourceAsStream( propertyFileName);// 属性文件输入流 // is = new FileInputStream("src/" + propertyFileName); prop.load(is);// 将属性文件流装载到Properties对象中 is.close();// 关闭流 dbClassName = prop.getProperty("dbClassName"); dbUrl = prop.getProperty("dbUrl"); dbUser = prop.getProperty("dbUser"); dbPwd = prop.getProperty("dbPwd"); } catch (Exception e) { System.out.println("属性文件 " + propertyFileName + " 打开失败!"); } try { Class.forName(dbClassName);// 1.注册驱动 } catch (ClassNotFoundException e) { e.printStackTrace(); } } public JDBConnection() {// 默认的不带参数的构造函数 try { Class.forName(dbClassName);// 1.注册驱动 } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection() { Connection conn = null; try { // Class.forName(dbClassName);// 1.注册驱动 conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);//2.建立与数据库的链接 } catch (Exception ee) { ee.printStackTrace(); } if (conn == null) { System.err .println("警告: DbConnectionManager.getConnection() 获得数据库链接失败.\r\n\r\n链接类型:" + dbClassName + "\r\n链接位置:" + dbUrl + "\r\n用户/密码" + dbUser + "/" + dbPwd); } return conn; } /* * 功能:执行查询语句 */ public ResultSet executeQuery(String sql) { try { // 捕捉异常 conn = getConnection(); // 调用getConnection()方法构造Connection对象的一个实例conn stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,//3.创建语句 ResultSet.CONCUR_READ_ONLY); rs = stmt.executeQuery(sql);//4.执行查询 } catch (SQLException ex) { System.err.println(ex.getMessage()); // 输出异常信息 } return rs; // 返回结果集对象 5.结果处理 } /* * 功能:执行更新操作 */ public int executeUpdate(String sql) { int result = 0; // 定义保存返回值的变量 try { // 捕捉异常 conn = getConnection(); // 调用getConnection()方法构造Connection对象的一个实例conn stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); result = stmt.executeUpdate(sql); // 执行更新操作 } catch (SQLException ex) { result = 0; // 将保存返回值的变量赋值为0 } return result; // 返回保存返回值的变量 } /* * 功能:关闭数据库的连接 */ public void close() {//6.释放资源 try { // 捕捉异常 try { if (rs != null) { // 当ResultSet对象的实例rs不为空时 rs.close(); // 关闭ResultSet对象 } } finally { try { if (stmt != null) { // 当Statement对象的实例stmt不为空时 stmt.close(); // 关闭Statement对象 } } finally { if (conn != null) { // 当Connection对象的实例conn不为空时 conn.close(); // 关闭Connection对象 } } } } catch (Exception e) { e.printStackTrace(System.err); // 输出异常信息 } }} |
如您还有不明白的可以在下面与我留言或是与我探讨QQ群308855039,我们一起飞!
相关文章
- Spring Boot中对接Twilio以实现发送验证码和验证短信码
- Spring Boot 3.5:这次更新让你连配置都不用写了,惊不惊喜?
- Spring Boot+Pinot实战:毫秒级实时竞价系统构建
- SpringBoot敏感配置项加密与解密实战
- SpringBoot 注解最全详解,建议收藏!
- Spring Boot 常用注解大全:从入门到进阶
- SpringBoot启动之谜:@SpringBootApplication如何让配置化繁为简
- Springboot集成Kafka原理_spring集成kafka的原理
- Spring Boot中@Data注解的深度解析与实战应用
- 大佬用1000字就把SpringBoot的配置文件讲的明明白白!
