各位大侠帮忙看看哪不对啊,总是实现不了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);
}
}
}
------解决方案--------------------
JAVA的去JAVA问问