web程序_通过学号查询学生信息_笔记

在之前的博客中我写过关于简易的查询四级成绩的web程序,在这里,我们连接了数据库,写一个通过输入学生的学号查询学生成绩的web小程序
其实这是非常简单的,大体就是在之前的小程序上加了一个jdbc,从显示随机数到显示数据库中的内容。两个程序大部分内容相似度还是非常高的,就是在选择继承或者实现servlet类/接口方面发生了改变。四级是实现Servlet接口,学生信息查询是继承HttpServlet类,也不用再去写web.xml文件了。

1、连接数据库

package com.qdu.dao;

import java.sql.Connection;
import java.sql.DriverManager;
//建立与数据库的连接
public class BaseDao {
	private static final String url="jdbc:sqlserver://localhost:1433;databaseName=QDU";
	public static Connection getConnection(){
		try {
		Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		return DriverManager.getConnection(url, "sa", "123456");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("抱歉,数据库连接出错!");
			System.exit(0);
		} 
		return null;
	}
}

package com.qdu.dao;

import com.qdu.pojo.Student;
import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/*
 * Dao类-数据访问对象类,专门用于访问数据库中的表 StudentDao包含方法用于对StudentMark表执行增删改查,
 * 这里只封装了一个方法用于查询
 */
public class StudentDao {

    /*
     * 根据学号查询学生所有信息
     * param rollno 学生学号
     * return 包含学生所有信息的学生对象
     */
    public Student findStudentByRollno(String rollno) {

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            con = BaseDao.getConnection();
            ps = con.prepareStatement("select * from Student where Srollno=?");
            ps.setString(1, rollno);
            rs = ps.executeQuery();

            if (rs.next()) {
                return new Student(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5));
            } 
            ps.close();
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        } 
        return null;
    }
}

2、实体类


package com.qdu.pojo;

/*
 * 学生实体类,用于存储学生所有信息
 */
public class Student {
    
    private String srollno;
    private String sname;
    private String sgender;
    private String sbatch;
    private String spassword;
    
    public Student() {
    }

    public Student(String srollno, String sname,String spassword, String sgender, String sbatch) {
        this.srollno = srollno;
        this.sname = sname;
        this.sgender = sgender;
        this.sbatch = sbatch;
        this.spassword = spassword;
    }

    public String getSrollno() {
        return srollno;
    }

    public void setSrollno(String srollno) {
        this.srollno = srollno;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSgender() {
        return sgender;
    }

    public void setSgender(String sgender) {
        this.sgender = sgender;
    }

    public String getSbatch() {
        return sbatch;
    }

    public void setSbatch(String sbatch) {
        this.sbatch = sbatch;
    }

    public String getSpassword() {
        return spassword;
    }

    public void setSpassword(String spassword) {
        this.spassword = spassword;
    }
}

3、Servlet类


package com.qdu.pojo;

import com.qdu.dao.StudentDao;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/ls")
public class StudentServlet extends HttpServlet {
//处理页面发送的请求信息等
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StudentDao s = new StudentDao();
        String id = req.getParameter("id");
        Student stu = s.findStudentByRollno(id);

        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        if (stu != null) {
            out.println("
"); out.println("

学籍信息

"); out.println(""); out.println(""); out.println(""+""+""+""+ ""); out.println("
Sname: Rollno: Password: Gender: Batch:
" + stu.getSname() + "" + stu.getSrollno() + "" + stu.getSpassword() + "" + stu.getSgender() + "" + stu.getSbatch() + "
"); out.println("
"); } else { out.println("
"); out.println("

无该学生信息

"); out.println("

"); } } }

你可能感兴趣的:(IT,编程,jsp,servlet,web)