开发者社区> 问答> 正文

PreparedStatement接口的常用方法及其描述是什么呢?

PreparedStatement接口的常用方法及其描述具体内容是的什么呢?

展开
收起
叫我饭啊啊 2021-09-29 17:25:32 681 0
1 条回答
写回答
取消 提交回答
  • 通过增删查改 4个最常用的功能讲吧。 1 对于增删改来说 但是如果sql语句有?参数的话,需要先用到preparedStatement.setInt(1,参数值), 或者preparedStatement.setString(1,参数值)等等(那几种基本类型) 。 其中1表示第几个?号. 最后调用preparedStatement.executeUpdate()方法; 2 对于查来说 有参数的话同上。 最后调用preparedStatement.executeQuery(); 3 最后附上一个连接JDBC使用sql的方法(mysql数据库)。 一 使用preparedStatement.executeQuery()来查找用户 private static final String FIND_BY_EMAIL ="select * from user where email=?"; public User findByEmail(String email) throws Exception { PreparedStatement statement=getConnection().prepareStatement(FIND_BY_EMAIL); statement.setString(1, email); ResultSet rs=statement.executeQuery(); User user=null; if(rs.next()){ user=new User(); user.setEmail(rs.getString("email")); user.setId(rs.getInt("id")); user.setPassword(rs.getString("password")); } return user; } 二 使用preparedStatement.executeUpdate()方法删除一个用户 private static final String DELETE_USER_BY_ID="delete from user where id=?"; public void delete(int id) throws Exception(){ PreparedStatement statement=getConnection().prepareStatement(FIND_BY_EMAIL); statement.setInt(1, id); ResultSet rs=statement.executeUpdate(); }

    2021-09-29 17:47:47
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spark SQL: Past, Present and Future 立即下载
Spark SQL:Past Present &Future 立即下载
低代码开发师(初级)实战教程 立即下载