关于JSP,程序设计。。。跪求代码。。在线等着中…………

2024-11-18 01:40:56
推荐回答(2个)
回答1:

我这个是jsp+servlet+MySQL做出来的不知道对你的要求合不合适!
呵呵 要是不合适你可以在线咪下我!!呵呵

1.先连接数据库!我不知道你是什么数据库我的是MySQL的我先我的吧!!

package com.softeem.connection;

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

public class DBConnection {
private static final String URL="jdbc:mysql://localhost:3306/user";
private static final String NAME="root";
private static final String PASSWORD="root";
private static final String DRIVERCLASS="com.mysql.jdbc.Driver";

static{
try {
Class.forName(DRIVERCLASS);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException{
return DriverManager.getConnection(URL, NAME, PASSWORD);
}
public static void close(ResultSet rs,PreparedStatement pstat,Connection conn) throws SQLException{
if(rs!=null){
rs.close();
}if(pstat!=null){
pstat.close();
}if(conn!=null){
conn.close();
}

}

}
2.建个DAO吧!这样会比较的清晰点!!
package com.softeem.dao;

import java.util.List;

import com.softeem.dto.Student;

public interface IStudentDAO {
public Student findByName(String name,String password);

3.DAO实现类impl
package com.softeem.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.softeem.connection.DBConnection;
import com.softeem.dao.IStudentDAO;
import com.softeem.dto.Student;

public class StudentDAOImpl implements IStudentDAO {

public Student findByName(String name,String password) {
Connection conn=null;
PreparedStatement pstat=null;
ResultSet rs=null;
String sql="select student_name,student_password from t_student where student_name=?";
try {
conn=DBConnection.getConnection();
pstat=conn.prepareStatement(sql);
pstat.setString(1, name);
//pstat.setString(2, password);
rs=pstat.executeQuery();
Student student=new Student();
if(rs.next()){
student.setStudentName(rs.getString("student_name"));
student.setStudentPassword(rs.getString("student_password"));

}
return student;
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
DBConnection.close(rs, pstat, conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;

}

4.servlet 中!!

package com.softeem.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.softeem.dao.IStudentDAO;
import com.softeem.dto.Student;
import com.softeem.impl.StudentDAOImpl;

public class LoginServlet extends HttpServlet{

/**
*
*/
private static final long serialVersionUID = 1L;
private IStudentDAO studentDAO=new StudentDAOImpl();

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gbk");
String name=request.getParameter("name");
String password=request.getParameter("pass");
Student student=studentDAO.findByName(name, password);
if(name.equals(student.getStudentName()) && password.equals(student.getStudentPassword())){
request.getRequestDispatcher("/success.jsp").forward(request, response);
}else{
request.getRequestDispatcher("/failure.jsp").forward(request, response);
}
}
}

5.jsp中的代码!!




login.html















用户名:
密 码:





呵呵 代码可能有点多 不过我觉得这样做思路会比较的清晰点 还有许多的简单方法可以做!!希望你能够采纳 呵呵!!
其中数据库和数据库中俄字段和你的不一样你自己改下应该就可以了!!!
还要写有一个类与数据库中数据的相匹配的!!那个很简单的自己写吧!!

回答2:

也没有悬赏??