日期:2014-05-17 浏览次数:20731 次
Connection ct=null;
ResultSet rs=null;
PreparedStatement ps=null;
// String url="jdbc:oracle:thin:@127.0.0.1:1521:ORCLDJL";
// String dbuname="scott";
// String dbpasswd="tiger";
try {
//1.加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//2.得到连接
ct=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCLDJL","scott","tiger");
//3.创建PreparedStatement
ps=ct.prepareStatement("select * from users where id=? and passwd=?");
//给?赋值
ps.setObject(1, id);
ps.setObject(2, password);
//4.执行操作
rs=ps.executeQuery();
//5.根据结果做处理
System.out.println(rs.next());
if(rs.next()){
//说明该用户合法
System.out.println("ok!");
request.getRequestDispatcher("/MainFrame").forward(request, response);
}else{
System.out.println("no ok!");
request.getRequestDispatcher("/LoginServlet").forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭资源
try {
if (rs!=null) {rs.close();rs=null;}
if (ps!=null) {ps.close();ps=null;}
if (ct!=null) {ct.close();ct=null;}
} catch (SQLException e) {
e.printStackTrace();
}
}