会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 128778个问题

image.png

JAVA 全系列/第三阶段:数据库编程/JDBC技术 52楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术 54楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术 56楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术 57楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术 58楼

为什么我的fo-each遍历不出来



package com.itbaizhan;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
* 动态条件测试类
*/

public class DynamicConditionQueryTest {
   public List<Users> queryUsers(Users users){
       List<Users> list= new ArrayList<>();
       Connection conn =null;
       PreparedStatement ps = null;
       ResultSet rs = null;
       try{
           conn = JdbcUtils.getConnection();
           //拼接查询sql语句
           String sql = this.generate(users);
          // System.out.println(sql);
           ps = conn.prepareStatement(sql);
           //执行语句
           rs = ps.executeQuery();
           while (rs.next()){
               Users users1 = new Users();
               users1.setUserid(rs.getInt("userid"));
               users1.setUsername(rs.getString("username"));
               users1.setUserage(rs.getInt("userage"));
               list.add(users1);
           }
       }catch (Exception e){
           e.printStackTrace();
       }finally {
           JdbcUtils.closeResource(rs,ps,conn);
       }

       return list;
   }
   public String generate(Users users){
       StringBuilder sb = new StringBuilder("select * from users where 1=1 ");
       if (users.getUserid()>0){
           sb.append(" and userid = ").append(users.getUserid());
       }
       if (users.getUsername() != null && users.getUsername().length()>0){
           sb.append(" and username ='").append(users.getUsername()).append("'");
       }
       if (users.getUserage()>0){
           sb.append(" and userage = ").append(users.getUserage());
       }
       return sb.toString();
   }

   public static void main(String[] args) {
       DynamicConditionQueryTest d = new DynamicConditionQueryTest();
       Users users = new Users();
       users.setUserid(22);
       users.setUsername("yuan");
       List<Users> list = d.queryUsers(users);
       for (Users t:list){
           System.out.println(t.getUserid()+" "+t.getUsername()+" "+t.getUserage());
           System.out.println(" ni");
       }
   }
}


"C:\Program Files\Java\jdk-11.0.12\bin\java.exe" "-javaagent:D:\IntelliJ IDEA Community Edition 2023.1\lib\idea_rt.jar=65185:D:\IntelliJ IDEA Community Edition 2023.1\bin" -Dfile.encoding=UTF-8 -classpath D:\Mycode\jdbcdemo\out\production\jdbcdemoMysql;D:\Mycode\jdbcdemo\lib\mysql-connector-java-5.1.48.jar com.itbaizhan.DynamicConditionQueryTest


Process finished with exit code 0



JAVA 全系列/第三阶段:数据库编程/JDBC技术 59楼

哪里错了

package com.itbaizhan;

import java.io.*;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
* Blob类型操作测试类
*/
public class BlobTest {
   /**
    *movie表中插入数据
    */
   public void insertMoive(String moviename, InputStream is){
       Connection connection = null;
       PreparedStatement ps = null;
       try{
           //获取链接
           connection = JdbcUtils.getConnection();
           //获取PrepareStatement对象
           ps = connection.prepareStatement("insert into movie values(default,?,?)");
           //绑定参数
           ps.setString(1,moviename);
           ps.setBlob(2,is);
           ps.executeUpdate();
       }catch (Exception e){
           e.printStackTrace();
       }finally {
           JdbcUtils.closeResource(ps,connection);
       }
   }
   /**
    * 根据影片ID查询影片信息
    * @param movieid
    */
   public void selectMovieBlob(int movieid){
       Connection conn =null;
       PreparedStatement ps = null;
       ResultSet rs = null;
       try{
           //获取连接
           conn =JdbcUtils.getConnection();
           //创建PreparedStatement对象
           ps = conn.prepareStatement("select * from movie where movieid = ?");
           //绑定参数
           ps.setInt(1,movieid);
           //执行sql
           rs = ps.executeQuery();
           while(rs.next()){
               int id = rs.getInt("movieid");
               String name = rs.getString("moviename");
               System.out.println(id+" "+name);
               //获取blob类型的数据
               Blob blob = rs.getBlob("poster");
               //获取能够从Blob类型的列中读取数据的IO
               InputStream is = blob.getBinaryStream();
               //创建文件输出字节流对象
               OutputStream os = new FileOutputStream(id+"_"+name+".jpg");
               //操作流完成文件的输出处理
               byte[] buff = new byte[1024];
               int len;
               while((len = is.read(buff)) != -1){
                   os.write(buff,0,len);
               }
               os.flush();
               is.close();
               os.close();
           }
       }catch(Exception e){
           e.printStackTrace();
       }finally{
           JdbcUtils.closeResource(rs,ps,conn);
       }
   }


   public static void main(String[] args) throws FileNotFoundException {
       BlobTest b = new BlobTest();
//        InputStream is = new FileInputStream(new File("D:/截图/数据库/MySQLDML操作/MySql Blob类型.jpg"));
//        b.insertMoive("Blob类型",is);
       b.selectMovieBlob(1);
   }
}


java.io.FileNotFoundException: 1Blob??.jpg (文件名、目录名或卷标语法不正确。)

at java.base/java.io.FileOutputStream.open0(Native Method)

at java.base/java.io.FileOutputStream.open(FileOutputStream.java:298)

at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:237)

at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:126)

at com.itbaizhan.BlobTest.selectMovieBlob(BlobTest.java:60)

at com.itbaizhan.BlobTest.main(BlobTest.java:83)


Process finished with exit code 0


JAVA 全系列/第三阶段:数据库编程/JDBC技术 60楼

百战程序员微信公众号

百战程序员微信小程序

©2014-2024 百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637