添加mysql的驱动包,再通过hibernate的逆向工程连接数据库,在jsp页面中使用以下代码
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.sql.*;"%>
<%
String name = request.getParameter("name"); //获取用户名
byte b[]=name.getBytes("ISO-8859-1");
name=new String(b);
String password = request.getParameter("password"); //获取密码
%>
<%
try {Class.forName("com.mysql.jdbc.Driver");}
catch(ClassNotFoundException e){}
try{
String url="jdbc:mysql://localhost:3306/myuser";
Connection con=DriverManager.getConnection(url,"root","chhf1988");
Statement stm=con.createStatement();
String sql="select * from user where name="+"'"+name+"'"+"and password="+"'"+password+"'";
ResultSet rs=stm.executeQuery(sql);
if(rs.next())
{
out.println("欢迎 "+ rs.getString(2)+"用户!! ");
}
else{
out.println("用户名或密码错误!请重新输入!! ");
}
stm.close();
con.close();}
catch(SQLException e){}
%>