100分求解JSP登陆验证代码
要求:JSP+sevlet+javabean+orace的
数据库查询跟连接不能写在JSP中
有这个代码提供的朋友送100分,如果提供不了代码的朋友可以帮我找出本人写的所在错误的代码并修改,也给予100分!
QQ:350386127
E-MAIL:lanxinabc@163.com
------解决方案--------------------import java.sql.DriverManager;
import java.sql.Connection;
public class MyConnection {
public static java.sql.Connection getConnection() throws Exception {
Connection conn = null;
String url = "jdbc:oracle:thin:@localhost:1521:databaseName ";
String usr = "userName ";
String pass = "psw ";
Class.forName( "oracle.jdbc.OracleDriver ");
conn = DriverManager.getConnection(url, usr, pass);
return conn;
}
}
这个类我测试过的,你改一下相应参数,看能不能获取到Connection
------解决方案-------------------- <%@ page contentType= "text/html; charset=GBK " %>
<%@ page import = "java.sql.* "%>
<%Class.forName( "oracle.jdbc.driver.OracleDriver ").newInstance();
String url= "jdbc:oracle:thin:@192.168.1.13:1521:orcl ";
//orcl为你的数据库的SID
String user= "user ";
String password= "password ";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
request.setCharacterEncoding( "GB2312 ");
String admin_name = request.getParameter( "admin_name ");
String admin_pwd = request.getParameter( "admin_pwd ");
String sql = "select * from scott.admin where admin_name = ' " +admin_name+ " ' ";
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()){
if(rs.getString( "admin_pwd ").equals(admin_pwd)){
session.putValue( "admin ",admin_name);
session.putValue( "password ",admin_pwd);
response.sendRedirect( "/case/corpweb/page/main.jsp ");
}else{
out.print( " <script language= 'javascript '> document.write( '\u5bc6\u7801\u4e0d\u6b63\u786e\uff0c\u8bf7\u540e\u9000\u91cd\u586b\uff01 '); </script> ");
}
}else{
out.print( " <script language= 'javascript '> document.write( '\u8be5\u7528\u6237\u4e0d\u5b58\u5728\uff0c\u8bf7\u91cd\u65b0\u586b\u5199\uff01 '); </script> ");
}
%>
------解决方案-------------------- <%!
Connection con = null ;
PreparedStatement stmt = null ;
ResultSet rs = null ;
%>
<%
// 声明一个boolean变量,用于保存用户是否合法的状态
boolean flag = false ;
// 接收参数
String name= request.getParameter( "name ") ;
String password = request.getParameter( "psw ") ;
%>
<%
String sql = "SELECT name FROM info WHERE name=? and psw=? " ;
try
{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver ");
con=DriverManager.getConnection( "jdbc:odbc:jsjdj ");
stmt = con.prepareStatement(sql) ;
stmt.setString(1,name) ;
stmt.setString(2,password) ;
rs = stmt.executeQuery() ;
if(rs.next())
{
// 用户合法
flag = true ;
// 将用户名保存在session之中
session.setAttribute( "uname ",rs.getString(1)) ;