1、编写java类jdbc.java,内容如下:
package yy;
import java.io.*;
import java.sql.*;
public class jdbc {
public jdbc() {
}
Connection conn = null ;
public java.sql.Connection getConn(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
conn= DriverManager.getConnection("jdbc:odbc:yyForum","xyworker","999");
}
catch(Exception e){
e.printStackTrace();
System.out.println("LICHAO");
}
return this.conn ;
}
public String ex_chinese(String str){
if(str==null){
str ="" ;
}
else{
try {
str = new String(str.getBytes("iso-8859-1"),"gb2312") ;
}
catch (Exception ex) {
}
}
return str ;
}
}
将jdbc.java编译成jdbc.class,并连包(yy)一起将jdbc.class复制到%tomcat%webapps/ROOT/WEB-INF/classes目录中。
2、建立ODBC数据源,关联到正确的数据库
3、在.jsp文件中使用语句即可,如下所示:
User_Name=request.getParameter("UserName");
Pass_Word=request.getParameter("password");
if ((User_Name.equals("")) || (Pass_Word.equals("")))
response.sendRedirect("err.jsp?id=2");
else
{
Connection con=yy.getConn();
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=null;
sql="Select * From 用户表 Where 用户名='"+User_Name+"' And 用户密码='"+Pass_Word+"'";
rs=stmt.executeQuery(sql);
rs.last();
//out.println(sql);
if (rs.getRow()>0)
{
response.sendRedirect("main.jsp");
}else
{
response.sendRedirect("err.jsp?id=4");
}
}
%>