首页 > 其他分享 >sumbs项目搭建

sumbs项目搭建

时间:2022-09-01 11:59:33浏览次数:44  
标签:preparedStatement 项目 resultSet properties connection static null sumbs 搭建

在dao下新建BaseDao类用于读取上面的数据库配置文件

点击查看代码
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
//操作数据库的公共类
     public class BaseDao {     
         private static String driver;
         private static String url;
         private static String username;
         private static String password;
     
         //静态代码块,类加载的时候就初始化了
         static {
             Properties properties = new Properties();
             //通过类加载器读取对应的资源
             InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
     
             try {
                 properties.load(is);
             } catch (IOException e) {
                 e.printStackTrace();
             }
     
             driver = properties.getProperty("driver");
             url = properties.getProperty("url");
             username = properties.getProperty("username");
             password = properties.getProperty("password");
         }
     
         //获取数据库的链接
         public static Connection getConnection(){
             Connection connection = null;
             try {
                 Class.forName(driver);
                 connection = DriverManager.getConnection(url, username, password);
             } catch (Exception e) {
                 e.printStackTrace();
             }
             return connection;
         }
     
         //编写查询公共方法
         public static ResultSet execute(Connection connection,String sql,Object[] params,ResultSet resultSet,PreparedStatement preparedStatement) throws SQLException {
             //预编译的sql,在后面直接执行就可以了
             preparedStatement = connection.prepareStatement(sql);
     
             for (int i = 0; i < params.length; i++) {
                 //setObject,占位符从1开始,但是我们的数组是从0开始!
                 preparedStatement.setObject(i+1,params[i]);
             }
     
             resultSet = preparedStatement.executeQuery();
             return resultSet;
         }
     
     
         //编写增删改公共方法
         public static int execute(Connection connection,String sql,Object[] params,PreparedStatement preparedStatement) throws SQLException {
             preparedStatement = connection.prepareStatement(sql);
     
             for (int i = 0; i < params.length; i++) {
                 //setObject,占位符从1开始,但是我们的数组是从0开始!
                 preparedStatement.setObject(i+1,params[i]);
             }
     
             int updateRows = preparedStatement.executeUpdate();
             return updateRows;
         }
     
     
         //释放资源
         public static boolean closeResource(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet){
             boolean flag = true;
     
             if (resultSet!=null){
                 try {
                     resultSet.close();
                     //GC回收
                     resultSet = null;
                 } catch (SQLException e) {
                     e.printStackTrace();
                     flag = false;
                 }
             }
     
             if (preparedStatement!=null){
                 try {
                     preparedStatement.close();
                     //GC回收
                     preparedStatement = null;
                 } catch (SQLException e) {
                     e.printStackTrace();
                     flag = false;
                 }
             }
     
             if (connection!=null){
                 try {
                     connection.close();
                     //GC回收
                     connection = null;
                 } catch (SQLException e) {
                     e.printStackTrace();
                     flag = false;
                 }
             }
             return flag;
         }
     }


有空看下数据库链接视频

标签:preparedStatement,项目,resultSet,properties,connection,static,null,sumbs,搭建
From: https://www.cnblogs.com/ahhh7931/p/16403172.html

相关文章

  • SAP 项目经理/FICO 顾问/ 权限顾问-广州外企需求 -WX lds1330
    公司属于外企新能源企业(工作地点广州市),目前市场增长强劲,需以下人员: ~~广州外企甲方SAP项目经理-5年以上,英语流利-有过外企或SAP项目管理经验,沟通能力好,熟悉制造行业后......
  • SpringBoot项目引入Swagger接口文档
    一、在项目中引入Swagger依赖<!--swagger--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifa......
  • 界面组件DevExpress MAUI & Xamarin v22.1 - 全新的项目模板
    DevExpress拥有.NET开发需要的所有平台控件,包含600多个UI控件、报表平台、DevExpressDashboard eXpressApp 框架、适用于VisualStudio的CodeRush等一系列辅助工具。......
  • Alphabitz — 项目 A
    Alphabitz—项目AAlphabitz系列的第一个条目将是一个简单的算术应用程序,它通过非常简单的UI执行各种单位转换。这就像计算器应用程序的想法,但有更多在任何项目中有......
  • VSCode创建Vue项目完整教程
    VSCode创建Vue项目完整教程文章目录一、配置环境1.安装VSCode2.安装node.js3.安装配置脚手架vue-cli二、创建vue项目1.命令方式创建2.重新初始化依赖3.启动项目......
  • 软考-高项-第四章 项目整体管理
     十大管理图镇楼 整体管理概述项目整体管理知识领域包括识别、定义、结合、统一与协调项目管理过程组内不同过程与项目管理活动所需进行的各种过程和活动时一个......
  • Maven项目创建并打印出Hello worlld
    主要分为两大部,①创建Maven项目,②在项目中打印一个Helloworlld。Step1.打开IDEA欢迎界面点击“NewProject”,创建新项目。 Step2.创建Maven项目,jdk选择1.8版本(看......
  • springboot+Vue项目允许跨域
    packagecom.example.demo.itkb.user.config;importorg.springframework.context.annotation.Configuration;importorg.springframework.web.servlet.config.annotat......
  • 如何把闲置的 Mac mini 搭建成一个局域网中的 Web 服务器 All In One
    如何把闲置的Macmini搭建成一个局域网中的Web服务器AllInOneMacmini2018https://secure5.www.apple.com.cn/shop/account/homehttps://mysupport.apple.com......
  • Python极客项目编程 中文PDF完整版入门到精通
     《Python极客项目编程》中文PDF完整版免费下载地址内容简介  · · · · · · Python是一种强大的编程语言,容易学习而且充满乐趣。但掌握了基本知识后,......