日期:2014-05-20  浏览次数:20912 次

各位大侠帮忙看看下面代码哪错了啊,应该怎么实现PreparedStatement优化啊
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.PreparedStatement;


public class SQLJdbc {
public static void main(String[] args) throws Exception {
read("a");

}

static void read(String user) {

java.sql.PreparedStatement ps = null;//预处理
ResultSet rs = null;
Connection conn = null;

try{


conn = JdbcUtils.getConnection();
//创建语句
String sql = "select dname,deptno,loc from dept where danme =?";
ps = conn.prepareStatement(sql);
ps.setString(1, user);
rs = ps.executeQuery();
//处理结果
System.out.println("-----------------");
System.out.println("执行结果如下所示:");
System.out.println("-----------------");
System.out.println(" 学号" + "\t" + " 姓名 " + "\t" + "工作");
System.out.println("-----------------");
String name = null;
String loc = null;
while(rs.next()){
loc = rs.getString("loc");
name = rs.getString("dname");
System.out.println(rs.getString("deptno")+"\t" + name + "\t" + loc);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
JdbcUtils.free(rs, ps, conn);
}
}

}

------解决方案--------------------
你先把错误贴出来,哥们,我就知道你什么地方错误了~!这么看,看死人去哒,我表示抗议~~~!
------解决方案--------------------
这个方法是static????
------解决方案--------------------
public static void main(String[] args) throws Exception {...}

请问你在main函数中使用throws是想将异常忘哪里抛?错了。。

其他代码没看出错误。。就看看连接数据库有没有错或者查询的表和字段是否正确。。