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

2017-11-28 15:02:17 +08:00
 choice4

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
 */

}

2363 次点击
所在节点    Java
9 条回复
choice4
2017-11-28 15:02:49 +08:00
为什么 ps.setString()不起作用。。数据库是 mysql
SuperMild
2017-11-28 15:07:25 +08:00
rs.next()执行了两次
Hzzone
2017-11-28 15:09:08 +08:00
rs.next()执行了两次
choice4
2017-11-28 15:12:54 +08:00
哇感谢感谢 糗了 不过控制台输出 syso(sb)为什么里边还是问号啊 我以前看视频跟着打的时候当传进参数输出问号就会被赋值输出来啊?这个咋回事
tianshuang
2017-11-28 15:15:04 +08:00
这代码块,还是用 Java 7+ 支持 auto close 的 try catch 吧。
choice4
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
2017-11-28 15:16:35 +08:00
对。。myeclipse 里自带的 jdk 懒得弄了。。
SuperMild
2017-11-28 15:24:40 +08:00
@choice4 你只对 ps 做了操作,没有对 sb 做操作啊
choice4
2017-11-28 15:30:20 +08:00
哇 难了半天来 v2 秒解 感谢大佬感谢大佬

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/410201

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX