//bbsreg.java
package twm;
import java.sql.*;public class bbsreg {
String sDBDriver;
String sConncStr;
Connection conn;
ResultSet rs;
public bbsreg(){
String url="jdbc:mysql://localhost:3306/bbs";
sDBDriver="com.mysql.jdbc.Driver";
conn=null;
rs=null;
String user="root";
String password="6949453";
try{
Class.forName(sDBDriver);//加载数据库
conn=DriverManager.getConnection(url,user,password);//连接数据库
System.out.println("数据库连接成功!");可以打印输出 }catch(ClassNotFoundException e){
System.out.println(e.getMessage());
}catch(SQLException e)
{System.out.println("cannot connect the sql");}
}
public ResultSet executeQuery(String sql){ //创建查询结果集
rs=null;
try{
Statement statement=conn.createStatement();
rs=statement.executeQuery(sql);
System.out.println("正在查询!");不执行!! }catch(SQLException e){
System.out.println(e.getMessage());
}
return rs;
}
public void executeUpdate(String sql){
try{
Statement statement=conn.createStatement();
statement.executeUpdate(sql);
}catch(SQLException e){
System.out.println(e.getMessage());
}
}
}
--------------------------------------------------------------------------------------------------------
注册页面跳转到此页面。
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.sql.*" %>
<jsp:useBean id="conn" class="twm.bbsreg" scope="page"/>
<% %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'chklogin.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<%request.setCharacterEncoding("gb2312");
String sql="";
ResultSet rs;
String username=request.getParameter("username");
String password=request.getParameter("password");
//out.println(username+password);
if(username!=null&&username.equals(" "))
try{
sql="select * from user where username='"+username+"'and userpassword='"+password+"'";
rs=conn.executeQuery(sql);
System.out.println("调用JavaBean!");调用JavaBean!打印不出来[/
if(rs.next()){
// session.setAttribute("login","ok");
//session.setAttribute("username",username);
%>
<jsp:forward page="index1.jsp"></jsp:forward>
若无法跳转,请点击<a href="index1.jsp">这里</a>
<%}else
out.println("错误的用户名或密码!");
out.println("<a href=login.jsp>返回</a>");
}catch(Exception e){
e.printStackTrace();} %>
</body>
</html>
package twm;
import java.sql.*;public class bbsreg {
String sDBDriver;
String sConncStr;
Connection conn;
ResultSet rs;
public bbsreg(){
String url="jdbc:mysql://localhost:3306/bbs";
sDBDriver="com.mysql.jdbc.Driver";
conn=null;
rs=null;
String user="root";
String password="6949453";
try{
Class.forName(sDBDriver);//加载数据库
conn=DriverManager.getConnection(url,user,password);//连接数据库
System.out.println("数据库连接成功!");可以打印输出 }catch(ClassNotFoundException e){
System.out.println(e.getMessage());
}catch(SQLException e)
{System.out.println("cannot connect the sql");}
}
public ResultSet executeQuery(String sql){ //创建查询结果集
rs=null;
try{
Statement statement=conn.createStatement();
rs=statement.executeQuery(sql);
System.out.println("正在查询!");不执行!! }catch(SQLException e){
System.out.println(e.getMessage());
}
return rs;
}
public void executeUpdate(String sql){
try{
Statement statement=conn.createStatement();
statement.executeUpdate(sql);
}catch(SQLException e){
System.out.println(e.getMessage());
}
}
}
--------------------------------------------------------------------------------------------------------
注册页面跳转到此页面。
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.sql.*" %>
<jsp:useBean id="conn" class="twm.bbsreg" scope="page"/>
<% %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'chklogin.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<%request.setCharacterEncoding("gb2312");
String sql="";
ResultSet rs;
String username=request.getParameter("username");
String password=request.getParameter("password");
//out.println(username+password);
if(username!=null&&username.equals(" "))
try{
sql="select * from user where username='"+username+"'and userpassword='"+password+"'";
rs=conn.executeQuery(sql);
System.out.println("调用JavaBean!");调用JavaBean!打印不出来[/
if(rs.next()){
// session.setAttribute("login","ok");
//session.setAttribute("username",username);
%>
<jsp:forward page="index1.jsp"></jsp:forward>
若无法跳转,请点击<a href="index1.jsp">这里</a>
<%}else
out.println("错误的用户名或密码!");
out.println("<a href=login.jsp>返回</a>");
}catch(Exception e){
e.printStackTrace();} %>
</body>
</html>
System.out.println("调用JavaBean!");
位置换一下,在运行一下。看能不能打印。
如果可以的话 就是 rs=conn.executeQuery(sql);语句有问题
慢慢调试 才知道问题出在哪!