jdbc的工具类封装
来源:互联网 发布:电脑专业录音软件 编辑:程序博客网 时间:2024/06/10 13:05
1 . 对于每个类基本要重复使用的代码,我们可以进行封装在一个类中,使用的时候直接调用这个类的方法就行了.
2 . 下面是工具封装类的三种方式.
第一种方法:
public static Connection getconn() { Connection connection = null; try { Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://localhost:3306/LM02"; connection = DriverManager.getConnection(url,"root","123"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return connection; }
第二种:
//按照流的方式读取 @Test public void test1() throws IOException { //1.获取属性对象properties Properties properties =new Properties(); //1.1 把属性文件资源配置当作流进行读取,参数只需要写相对src的目录 //getResourceAsStream()读指定文件内容以流的形式,该方法是ClassLoader类中 (装载class文件) // TestReadProperties.class.getClassLoader()获取当前类的类加载器对象 InputStream inStream = TestReadProperties.class.getClassLoader().getResourceAsStream("dbinfo.properties"); //2.装载属性文件 properties.load(inStream); String driverClass = properties.getProperty("driverClass"); String url = properties.getProperty("url"); String user = properties.getProperty("user"); String password = properties.getProperty("password"); System.out.println(driverClass+"=="+url); System.out.println(user+"=="+password); }
第三种:
//第三种封装了第二种 @Test public void test2() { //获取资源文件对象(参数只填文件名,相对路径) ResourceBundle resourceBundle =ResourceBundle.getBundle("dbinfo"); System.out.println("===="+resourceBundle.getString("driverClass")); }
注意上面的路径是相对路径,是相对于src来说的
阅读全文
0 0
- 封装的JDBC工具类
- JDBC工具类的封装
- jdbc工具类的封装
- jdbc的工具类封装
- jdbc封装工具类
- JDBC封装工具类
- JDBC 封装工具类
- JDBC封装工具类
- 一个简单封装的jdbc工具类
- JDBC工具类的封装以及运用
- Mysql_封装jdbc工具类
- 对JDBC操作数据库的工具类的封装
- JDBC的配置文件与工具类的封装
- JDBC的封装类
- JDBC入门(二)工具类封装
- JDBC的封装类构建
- 关于jdbc的配置文件的编写和工具类的封装
- JDBC的工具类
- windows下的Zcash钱包(ZEC钱包)-zcash4win 1.0.11
- 人脸识别
- gdal图像分块处理
- ngrx api
- 基于opencv的图像拼接(一): sift特征点提取
- jdbc的工具类封装
- 时间调度
- 织梦分页“共0页/0条记录”修改
- 入门 Vue.js会碰到的常见问题
- 002、java数据类型
- 工科硕士论文评阅面面观
- centos7firewall防火墙操作
- Mysql only_full_group_by以及其他关于sql_mode原因报错详细解决方案
- C标准库常用函数实现