10
回答
jsp用javabean连接mysql数据库
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

编出的代问题多多

这是一个登陆及验证界面,链接mysql数据库出问题了

ConfirmBean.java

package bean;
import java.io.*;
import java.sql.*;
public class ConfirmBean {
public Connection con=null;
public ResultSet rs=null;
private String DatabaseDriver="com.mysql.jdbc.Driver";
private String DatabaseConnStr="'jdbc:mysql://localhost/educatsystem','root','stevens'";

//定义方法 
/*setXxx用于设置属性值;getXxx用于得到属性值*/ 
public void setDatabaseDriver(String Driver){ 
this.DatabaseDriver=Driver; 

public String getDatabaseDriver(){ 
return (this.DatabaseDriver); 



    public void setDatabaseConnStr(String ConnStr){ 
this.DatabaseConnStr=ConnStr; 

public String getDatabaseConnStr(){ 
return (this.DatabaseConnStr); 



    public ConfirmBean(){//构造函数 
try{ 




Class.forName(DatabaseDriver); 

catch(java.lang.ClassNotFoundException e){ 
System.err.println("加载驱动器有错误:"+e.getMessage( )); 
System.out.print("执行插入有错误:"+e.getMessage());//输出到客户端 




public String getString(String name) throws SQLException{
       if(rs==null) throw new SQLException("ResultSet is null");
       return String.valueOf(rs.getString(name));
                    }                 
public void executeInsert(String sql){ 
try{ 
con = DriverManager.getConnection("jdbc:mysql://localhost/educatsystem","root","stevens");
Statement stmt=con.createStatement( ); 
stmt.executeUpdate(sql); 

catch(SQLException ex){ 
System.err.println("执行插入有错误:"+ex.getMessage() ); 
System.out.print("执行插入有错误:"+ex.getMessage());//输出到客户端 


// display data 


 
public ResultSet executeQuery(String sql){ 
rs=null; 
try{ 
con = DriverManager.getConnection("jdbc:mysql://localhost/educatsystem","root","stevens");
Statement stmt=con.createStatement( ); 
rs=stmt.executeQuery(sql);

catch(SQLException ex){ 
System.err.println("执行查询有错误:"+ex.getMessage() ); 
System.out.print("执行查询有错误:"+ex.getMessage()); //输出到客户端 

return rs; 

// delete data 
public void executeDelete(String sql){ 
try{ 


con = DriverManager.getConnection("jdbc:mysql://localhost/educatsystem","root","stevens");
Statement stmt=con.createStatement( ); 
stmt.executeUpdate(sql); 

catch(SQLException ex){ 
System.err.println("执行删除有错误:"+ex.getMessage() ); 
System.out.print("执行删除有错误:"+ex.getMessage()); //输出到客户端 



public void CloseDataBase(){ 
try{ 
con.close(); 

catch(Exception end){ 
System.err.println("执行关闭Connection对象有错误:"+end.getMessage( ) ); 
System.out.print("执行执行关闭Connection对象有错误:有错误:"+end.getMessage()); //输出到客户端 

}
}


login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>登陆</title>
    <style type="text/css">
      body {
           background-color: #9C3;
           }
    </style>
  </head>
  
  <body>
  <jsp:include page="head.jsp"></jsp:include><br/>
    <p><font color="blue" size=6>欢迎您登陆教师管理系统</font></p><br/>
    <form action="login_confirm.jsp" name="formlogin" method="post">
        登录名:<input type="text" name="name"><br/><br/>
        密     码:<input type="password" name="password"><br/><br/>
       <input type="submit" name="submit" value="登陆"> 
    </form>
  </body>
</html>


login_confirm.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>登陆检验</title>
 
  </head>
  
  <body>
<jsp:useBean id="db"  class="bean.ConfirmBean"> </jsp:useBean>
<%
             response.setContentType("text/html;charset=gb2312");
            
             request.setCharacterEncoding("gb2312");
             String name=request.getParameter("logname");
                       if(name==null)
                              {name="";
                               }
                        byte b[]=name.getBytes("gb2312");
                        name=new String(b);
             String password=request.getParameter("password");
                       if(password==null)
                              {password="";
                               }
                        byte c[]=name.getBytes("gb2312");
                        name=new String(c);
             
%>


<%
   String sql="INSERT INTO registmessage VALUES"+"("+"'"+name+"','"+password+")";
   db.getDatabaseConnStr();
   db.setDatabaseConnStr("'jdbc:mysql://localhost/educatsystem','root','stevens'");
   db.getDatabaseDriver();
   db.setDatabaseDriver("com.mysql.jdbc.Driver");
   db.executeInsert("sql");
   db.executeQuery("sql");
%>  
  </body>
</html>


<无标签>
举报
Russellll
发帖于5年前 10回/4K+阅
共有10个答案 最后回答: 5年前

ConfirmBean.java

package bean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ConfirmBean {

	public boolean testLogin(String name, String password) {
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		boolean flag = false;
		String sql = "SELECT * FROM registmessage WHERE name = '" + name + "'" + " AND password='" + password + "'";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://localhost/educatsystem", "root", "stevens");
		} catch (Exception e) {
			System.err.println("数据库连接加载异常:" + e.getMessage());
			e.printStackTrace();
		}

		try {
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			if (rs.next()) { // 查询结果有数据
				flag = true;
			}
		} catch (SQLException ex) {
			System.err.println("执行查询有错误:" + ex.getMessage());
			System.out.print("执行查询有错误:" + ex.getMessage()); // 输出到客户端
		} finally {
			try {
				stmt.close();
				rs.close();
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return flag;
	}

}

login_confirm.jsp

<%@ page language="java" pageEncoding="gb2312"%>
<%@page import="bean.ConfirmBean"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>

<html>
	<head>
		<base href="<%=basePath%>">
		<title>登陆检验</title>
	</head>

	<body>
		<%
			response.setContentType("text/html;charset=gb2312");
			request.setCharacterEncoding("gb2312");
			
			String name = request.getParameter("logname");
			String password = request.getParameter("password");
			if (name == null) {
				name = "";
				out.println("用户名为空");
			}
			if (password == null) {
				password = "";
				out.println("密码为空");
			}
			boolean flag = new ConfirmBean().testLogin(name,password);
			if(flag){
				out.println("登陆成功");
			}else{
				out.println("登陆失败");
			}
		%>

	</body>
</html>
没测试,可以试试~!~

--- 共有 1 条评论 ---
Russellll谢谢哈,刚才改好了 5年前 回复
db.executeInsert(sql);
db.executeQuery(sql);

这两个不用加引号,但是你sql是插入语句调用executeQuery没有意义


<jsp:useBean id="db" class="bean.ConfirmBean">
//需要跳转前传递过来,要不就是null了,后面都会报错
//例如:
request.setAttribute("db",new bean.ConfirmBean());
还是网上找个登陆的例子吧,数据库代码一般都再后台写,不太会写到JSP


顶部