JDBC类,对象,方法总结

1.properties类

Properties(Java.util.Properties),该类主要用于读取Java的配置文件,不同的编程语言有自己所支持的配置文件,配置文件中很多变量是经常改变的,为了方便用户的配置,能让用户够脱离程序本身去修改相关的变量设置。就像在Java中,其配置文件常为.properties文件,是以键值对的形式进行参数配置的。

driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/smbms?useSSL=false&useUnicode=true&characterEncoding=utf-8
user = root
password = 123456
static {
        Properties properties = new Properties();
        InputStream resource = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
        try {
            properties.load(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        driver = properties.getProperty("driver");
        url = properties.getProperty("url");
        user = properties.getProperty("user");
        password = properties.getProperty("password");
    }

getProperty ( String key),用指定的键在此属性列表中搜索属性。也就是通过参数
key ,得到 key 所对应的 value。

2.Connection

方法的作用:初始化给定的类。而我们给定的 MySQL 的 Driver 类中,它在静态代码块中通过 JDBC 的 DriverManager 注册了一下驱动。我们也可以直接使用 JDBC 的驱动管理器注册 mysql 驱动,从而代替使用 Class.forName。

//    获取数据库的连接
    public static Connection GetConnection() {
        Connection connection = null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, user, password);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }

Java DriverManager.getConnection() 方法用于获得试图建立到指定数据库 URL 的连接。DriverManager 试图从已注册的 JDBC 驱动程序集中选择一个适当的驱动程序。connection就是返回的连接对象

3.preparedStatement

Interface Statement
是一个接口,用于执行静态SQL语句并返回其生成的结果的对象。
Statement 和 PreparedStatement之间的关系和区别.
关系:PreparedStatement继承自Statement,都是接口
区别:PreparedStatement可以使用占位符,是预编译的,批处理比Statement效率高。SQL 语句被预编译并存储在 PreparedStatement 对象中,然后可以使用此对象多次高效地执行该语句。

//    编写查询公共类,将在sql语句中的条件组成一个数组,通过便利将他们传给preparedStatement,都提出来方便进行统一的关闭
    public static ResultSet Excute(Connection connection,String sql,Object[] params,ResultSet resultSet,PreparedStatement preparedStatement ) throws SQLException {
        preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < params.length; i++) {
            preparedStatement.setObject(i+1,params[i]);
        }
        resultSet = preparedStatement.executeQuery();
        return resultSet;
    }

connection.prepareStatement(String sql)
创建一个 PreparedStatement 对象来将参数化的 SQL 语句发送到数据库。
prepareStatement.execute()
执行此 PreparedStatement对象中的SQL语句,这可能是任何类型的SQL语句。
executeQuery()
执行此 PreparedStatement对象中的SQL查询,并返回查询 PreparedStatement的 ResultSet对象。
executeUpdate()
执行在该SQL语句PreparedStatement对象,它必须是一个SQL数据操纵语言(DML)语句,如INSERT , UPDATE或DELETE ; 或不返回任何内容的SQL语句,例如DDL语句。
setObject(int parameterIndex, Object x)
使用给定对象设置指定参数的值。

实现:
usercode 和 password 是上层传入dao层的已知值,由preparedStatement.setObject()方法将数组中的值传入preparedStatement(预编译语句)中,对应sql语句中的问号处。返回的的结果resultset是一个结果集对象,里面包含了在数据库查询到的结果。

String sql = "select * from smbms_user where userCode = ? && userPassword = ?";
Object[] params = {usercode, password};
resultSet = BaseDao.Excute(connection, sql, params, resultSet, preparedStatement);

封装了结果集的对象:内部有一个可移动的光标,默认情况,指向第一条记录集的上一条记录:
next();光标下移动一次:返回的boolean的值;判断是否有结果可以被遍历:
previous();光标上移动一次:
getObject(String columnLabel)获取此的当前行中指定列的值 ResultSet作为对象 Object在Java编程语言。
getInt(Int columnLabel) getString(String columnLabel)获取相应的值

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值