V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
choice4
V2EX  ›  Java

问个 jdbc 的不是伸手。。已经搜好长时间。。

  •  
  •   choice4 · 2017-11-28 15:02:17 +08:00 · 2359 次点击
    这是一个创建于 2333 天前的主题,其中的信息可能已经有所发展或是发生改变。

    package com.jdbc;

    import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;

    public class Select {

    public Student findById(String sno){
    	Connection conn = null;
    	PreparedStatement ps = null;
    	ResultSet rs = null;
    	Student s = new Student();
    	try {
    		conn = DBUtil.getConnection();
    		StringBuilder sb = new StringBuilder();
    		sb.append(" select * from student where SNO = ?");
    		ps = conn.prepareStatement(sb.toString());
    		ps.setString(1, sno);
    		rs = ps.executeQuery();
    		System.out.println(sb);
    		System.out.println(rs.next());
    		if(rs.next()){
    			s.setSno(rs.getString("SNO"));
    			s.setSname(rs.getString("SNAME"));
    			s.setAge(rs.getInt("AGE"));
    			s.setSex(rs.getString("AGR"));
    			s.setDept(rs.getString("DEPT"));
    			System.out.println(s.getSno());
    			return s;
    		}else{
    			return null;
    		}
    	} catch (Exception e) {
    		e.printStackTrace();
    		return null;
    	}finally{
    		if(rs != null){
    			try {
    				rs.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    			rs = null;
    		}
    		if(ps != null){
    			try {
    				ps.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    			ps = null;
    		}
    	}
    }
    
    
    public static void main(String[] args) {
    	Select s = new Select();
    	Student student = s.findById("1004");
    	System.out.println(student);
    }
    
    /**
     * 控制台输出:
     * select * from student where SNO = ?
     *true
     *null
     */
    

    }

    9 条回复    2017-11-28 15:30:20 +08:00
    choice4
        1
    choice4  
    OP
       2017-11-28 15:02:49 +08:00
    为什么 ps.setString()不起作用。。数据库是 mysql
    SuperMild
        2
    SuperMild  
       2017-11-28 15:07:25 +08:00   ❤️ 1
    rs.next()执行了两次
    Hzzone
        3
    Hzzone  
       2017-11-28 15:09:08 +08:00
    rs.next()执行了两次
    choice4
        4
    choice4  
    OP
       2017-11-28 15:12:54 +08:00
    哇感谢感谢 糗了 不过控制台输出 syso(sb)为什么里边还是问号啊 我以前看视频跟着打的时候当传进参数输出问号就会被赋值输出来啊?这个咋回事
    tianshuang
        5
    tianshuang  
       2017-11-28 15:15:04 +08:00
    这代码块,还是用 Java 7+ 支持 auto close 的 try catch 吧。
    choice4
        6
    choice4  
    OP
       2017-11-28 15:16:00 +08:00
    这是对比的那个
    List<Goddess> result = new ArrayList<Goddess>();
    Connection conn = DBUtil.getConnection();
    StringBuilder sb = new StringBuilder();
    sb.append(" select * from imooc_goddess where 1=1 ");
    if (params != null && params.size() > 0) {
    for (int i = 0; i < params.size(); i++) {
    Map<String, Object> map = params.get(i);
    sb.append(" and" + " " + map.get("name") + " "
    + map.get("rela") + " " + map.get("value"));
    }
    }
    PreparedStatement ptmt = conn.prepareStatement(sb.toString());
    System.out.println(sb.toString());


    主函数里是
    List<Map<String, Object>> params = new ArrayList<Map<String, Object>>();
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("name", "user_name");
    map.put("rela", "=");
    map.put("value", "'木冰眉'");
    params.add(map);

    List<Goddess> result = action.query(params);
    for (int i = 0; i < result.size(); i++) {
    System.out.println(result.get(i).getId() + ":"
    + result.get(i).getUser_name()
    + result.get(i).getBirthday());
    }

    /**
    * 控制台打印
    * select * from imooc_goddess where 1=1 and user_name = '木冰眉'
    *2:木冰眉 2000-05-02
    */
    choice4
        7
    choice4  
    OP
       2017-11-28 15:16:35 +08:00
    对。。myeclipse 里自带的 jdk 懒得弄了。。
    SuperMild
        8
    SuperMild  
       2017-11-28 15:24:40 +08:00   ❤️ 1
    @choice4 你只对 ps 做了操作,没有对 sb 做操作啊
    choice4
        9
    choice4  
    OP
       2017-11-28 15:30:20 +08:00
    哇 难了半天来 v2 秒解 感谢大佬感谢大佬
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5307 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 09:09 · PVG 17:09 · LAX 02:09 · JFK 05:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.