成都大学心理健康测试系统 (部分源代码)

一、连接数据库:

package com.scujcc;

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

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import com.scujcc.Quiz;

public class DB {
    private Connection conn;
    public void connect()
    {
        Context ctx;
        try
        {
            ctx=new InitialContext();
            DataSource ds=(DataSource) ctx.lookup("java:comp/env/mypool");
            conn=ds.getConnection();
            System.out.println("成功获取连接"+conn);
        }
        catch(NamingException e)
        {
            e.printStackTrace();
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
    }
    
    /**
     * 此方法可向数据库中添加一个心理测试题目
     * @param quiz 即将添加的测试题目
     * @return 若添加成功则返回true,否则返回false
     */
    public int add1(Quiz quiz)
    {
        int result=-1;
        connect();
        try
        {
            PreparedStatement ps=conn.prepareStatement("update csone set c_content=?,c_answer=? where id=1");
            ps.setString(1, quiz.getTitle());
            ps.setString(2, quiz.getContent());
            int count=ps.executeUpdate();
            if(count>0)
            {
                ResultSet generatedKeys = ps.getGeneratedKeys();
                   if (generatedKeys.next()) {
                        result = generatedKeys.getInt(1);
                    }
            }
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if(null!=conn)
                {
                    conn.close();
                }
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
        }
        return result;
    }
    
    public int add2(Quiz quiz)
    {
        int result=-1;
        connect();
        try
        {
            PreparedStatement ps=conn.prepareStatement("update csone set c_content=?,c_answer=? where id=2");
            ps.setString(1, quiz.getTitle());
            ps.setString(2, quiz.getContent());
            int count=ps.executeUpdate();
            if(count>0)
            {
                ResultSet generatedKeys = ps.getGeneratedKeys();
                   if (generatedKeys.next()) {
                        result = generatedKeys.getInt(1);
                    }
            }
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if(null!=conn)
                {
                    conn.close();
                }
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
        }
        return result;
    }
    
    public int add3(Quiz quiz)
    {
        int result=-1;
        connect();
        try
        {
            PreparedStatement ps=conn.prepareStatement("update csone set c_content=?,c_answer=? where id=3");
            ps.setString(1, quiz.getTitle());
            ps.setString(2, quiz.getContent());
            int count=ps.executeUpdate();
            if(count>0)
            {
                ResultSet generatedKeys = ps.getGeneratedKeys();
                   if (generatedKeys.next()) {
                        result = generatedKeys.getInt(1);
                    }
            }
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if(null!=conn)
                {
                    conn.close();
                }
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
        }
        return result;
    }
    
    public int add4(Quiz quiz)
    {
        int result=-1;
        connect();
        try
        {
            PreparedStatement ps=conn.prepareStatement("update csone set c_content=?,c_answer=? where id=4");
            ps.setString(1, quiz.getTitle());
            ps.setString(2, quiz.getContent());
            int count=ps.executeUpdate();
            if(count>0)
            {
                ResultSet generatedKeys = ps.getGeneratedKeys();
                   if (generatedKeys.next()) {
                        result = generatedKeys.getInt(1);
                    }
            }
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if(null!=conn)
                {
                    conn.close();
                }
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
        }
        return result;
    }
}
package com.scujcc;

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

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import com.scujcc.Quiz;

public class DB2 {
    private Connection conn;
    public void connect()
    {
        Context ctx;
        try
        {
            ctx=new InitialContext();
            DataSource ds=(DataSource) ctx.lookup("java:comp/env/mypool");
            conn=ds.getConnection();
            System.out.println("成功获取连接"+conn);
        }
        catch(NamingException e)
        {
            e.printStackTrace();
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
    }
    
    /**
     * 此方法可向数据库中添加一个心理测试题目
     * @param quiz 即将添加的测试题目
     * @return 若添加成功则返回true,否则返回false
     */
    public int add1(Quiz quiz)
    {
        int result=-1;
        connect();
        try
        {
            PreparedStatement ps=conn.prepareStatement("update ceshitwo set c_content=?,c_answer=? where id=1");
            ps.setString(1, quiz.getTitle());
            ps.setString(2, quiz.getContent());
            int count=ps.executeUpdate();
            if(count>0)
            {
                ResultSet generatedKeys = ps.getGeneratedKeys();
                   if (generatedKeys.next()) {
                        result = generatedKeys.getInt(1);
                    }
            }
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if(null!=conn)
                {
                    conn.close();
                }
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
        }
        return result;
    }
    
    public int add2(Quiz quiz)
    {
        int result=-1;
        connect();
        try
        {
            PreparedStatement ps=conn.prepareStatement("update ceshitwo set c_content=?,c_answer=? where id=2");
            ps.setString(1, quiz.getTitle());
            ps.setString(2, quiz.getContent());
            int count=ps.executeUpdate();
            if(count>0)
            {
                ResultSet generatedKeys = ps.getGeneratedKeys();
                   if (generatedKeys.next()) {
                        result = generatedKeys.getInt(1);
                    }
            }
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if(null!=conn)
                {
                    conn.close();
                }
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
        }
        return result;
    }
    
    public int add3(Quiz quiz)
    {
        int result=-1;
        connect();
        try
        {
            PreparedStatement ps=conn.prepareStatement("update ceshitwo set c_content=?,c_answer=? where id=3");
            ps.setString(1, quiz.getTitle());
            ps.setString(2, quiz.getContent());
            int count=ps.executeUpdate();
            if(count>0)
            {
                ResultSet generatedKeys = ps.getGeneratedKeys();
                   if (generatedKeys.next()) {
                        result = generatedKeys.getInt(1);
                    }
            }
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if(null!=conn)
                {
                    conn.close();
                }
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
        }
        return result;
    }
    
    public int add4(Quiz quiz)
    {
        int result=-1;
        connect();
        try
        {
            PreparedStatement ps=conn.prepareStatement("update ceshitwo set c_content=?,c_answer=? where id=4");
            ps.setString(1, quiz.getTitle());
            ps.setString(2, quiz.getContent());
            int count=ps.executeUpdate();
            if(count>0)
            {
                ResultSet generatedKeys = ps.getGeneratedKeys();
                   if (generatedKeys.next()) {
                        result = generatedKeys.getInt(1);
                    }
            }
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if(null!=conn)
                {
                    conn.close();
                }
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
        }
        return result;
    }
}

二、servlet:

package com.scujcc;

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

/**
 * Servlet implementation class QuizServlet
 */
@WebServlet("/admin/quiz")
public class QuizServlet extends HttpServlet {
    private static final long serialVersionUID = 54115222L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public QuizServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
        DB d=new DB();
        d.connect();
        response.getWriter().append("数据库连接池测试");
    }

}

三、

心理测试后台

package com.scujcc;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rui.Choice;

/**
 * Servlet implementation class Ttest1
 */
@WebServlet("/admin/ttest1")
public class Ttest1 extends HttpServlet {
    private static final long serialVersionUID = 15424529677L;
List choices=new ArrayList();
    
    private void data2()
    {
        Connection conn=null;
        ResultSet rs=null;
        try 
        {
            //1连接mysql
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://127.0.0.1:3306/jsp?serverTimezone=GMT";
            conn=DriverManager.getConnection(url, "root", "123456");
        
            
            //2执行sql
            String sql="select * from csone";
            PreparedStatement ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            
            //3读取结果
            while(rs.next())
            {
                Choice c= new Choice();
                c.setId(rs.getInt("id"));
                c.setContent(rs.getString("c_content"));
                c.setAnswer(rs.getString("c_answer"));
                choices.add(c);
            }
        }
        catch(ClassNotFoundException e)
        {
            System.out.println("找不到mysql驱动程序com.jdbc.Driver");
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if(rs!=null)
                {
                    rs.close();
                }
                if(conn!=null)
                {
                    conn.close();
                }            
            }
            catch(SQLException e)
            {
                System.out.print("关闭连接时出错了!");
            }
        }
    }
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Ttest1() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setCharacterEncoding("GB18030");
        response.getWriter().append("Served at: ").append(request.getContextPath());
        
        
        data2();
        request.setAttribute("choices", choices);
        
        getServletContext()
            .getRequestDispatcher("/shanchu1.jsp")
            .forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
        String choice=request.getParameter("choice");
        int choicei=0;
        if(null != choice)
        {
            choicei=Integer.parseInt(choice);
        }
    
        //根据choicei的值(可能为1,2,3,4。来输出其对应的答案)
        Choice userChoice=null;
        for(Object c: choices)
        {
            Choice cc=(Choice) c;
            if(cc.getId()==choicei)
            {
                    userChoice=cc;
                    break;
            }
        }
    
        
        
        

        
        String msg= "";
        int[] result = {0,0,0,0};
        int y=userChoice.getId();
            Delete1 d=new Delete1();
                if(y==1)
                {
                    result[0]=d.add1();
                }
                else if(y==2)
                {
                    result[1]=d.add2();
                }
                else if(y==3)
                {
                    result[2]=d.add3();
                }
                else if(y==4)
                {
                    result[3]=d.add4();
                }
//                if(result[m]>0)
//                {
//                    msg[m] = "成功添加测试题:" + quiz.getTitle()+"id为:"+result;
//                }
        PrintWriter out=response.getWriter();
        out.append(msg);
        request.setAttribute("msg", msg);
        getServletContext().getAttribute("userCounter");
        
        getServletContext().getRequestDispatcher("/admin/ttest1").forward(request, response);
    }

}
    private static final long serialVersionUID = 154529677L;
List choices=new ArrayList();
    
    private void data2()
    {
        Connection conn=null;
        ResultSet rs=null;
        try 
        {
            //1连接mysql
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://127.0.0.1:3306/jsp?serverTimezone=GMT";
            conn=DriverManager.getConnection(url, "root", "123456");
        
            
            //2执行sql
            String sql="select * from ceshitwo";
            PreparedStatement ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            
            //3读取结果
            while(rs.next())
            {
                Choice c= new Choice();
                c.setId(rs.getInt("id"));
                c.setContent(rs.getString("c_content"));
                c.setAnswer(rs.getString("c_answer"));
                choices.add(c);
            }
        }
        catch(ClassNotFoundException e)
        {
            System.out.println("找不到mysql驱动程序com.jdbc.Driver");
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if(rs!=null)
                {
                    rs.close();
                }
                if(conn!=null)
                {
                    conn.close();
                }            
            }
            catch(SQLException e)
            {
                System.out.print("关闭连接时出错了!");
            }
        }
    }
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Ttest2() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setCharacterEncoding("GB18030");
        response.getWriter().append("Served at: ").append(request.getContextPath());
        
        
        data2();
        request.setAttribute("choices", choices);
        
        getServletContext()
            .getRequestDispatcher("/shanchu2.jsp")
            .forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
        String choice=request.getParameter("choice");
        int choicei=0;
        if(null != choice)
        {
            choicei=Integer.parseInt(choice);
        }
    
        //根据choicei的值(可能为1,2,3,4。来输出其对应的答案)
        Choice userChoice=null;
        for(Object c: choices)
        {
            Choice cc=(Choice) c;
            if(cc.getId()==choicei)
            {
                    userChoice=cc;
                    break;
            }
        }
    
        
        
        

        
        String msg= "";
        int[] result = {0,0,0,0};
        int y=userChoice.getId();
            Delete2 d=new Delete2();
                if(y==1)
                {
                    result[0]=d.add1();
                }
                else if(y==2)
                {
                    result[1]=d.add2();
                }
                else if(y==3)
                {
                    result[2]=d.add3();
                }
                else if(y==4)
                {
                    result[3]=d.add4();
                }
//                if(result[m]>0)
//                {
//                    msg[m] = "成功添加测试题:" + quiz.getTitle()+"id为:"+result;
//                }
        PrintWriter out=response.getWriter();
        out.append(msg);
        request.setAttribute("msg", msg);
        getServletContext().getAttribute("userCounter");
        
        getServletContext().getRequestDispatcher("/admin/ttest2").forward(request, response);
    }

}

四、主页后台

package com.scujcc;

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

/**
 * Servlet implementation class Fhxszhuye2
 */
@WebServlet("/admin/fhxszhuye2")
public class Fhxszhuye2 extends HttpServlet {
    private static final long serialVersionUID = 767868767L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Fhxszhuye2() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
        getServletContext()
        .getRequestDispatcher("/WEB-INF/xszhuye2.jsp")
        .forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

 

转载于:https://www.cnblogs.com/Alei777/p/10946862.html

学生心理测验系统精简版(完全免费,无技术支持。2014年3月更新) 1.查看心理知识 2.向心理教师咨询心理问题:比如人际交往、家庭与婚姻、考试与学习、职业兴趣、性心理健康教育等等 3.进行心理测试,精简版免费提供以下测试:scl90心理生理卫生、你的包容力如何、自信心测试、“助人方面,你做得怎么样”。 完整版本还包括:艾森克情绪稳定性测验、艾森克人格问卷、交流恐惧自测量表结果、你能理解别人吗、人际关系综合诊断量表测试、你有社交恐惧症吗、人际应对能力自测、挫折折承受能力自测、适应能力测试、抑郁自评量表、你应对军训焦虑的能力、你意志品质如何、情绪紧张度自测、考试焦虑自评量表、焦虑自评量表、个人沟通技能自测量表、你是个高自我控制者吗、社会需要度测评、人际信任量表、通过测量了解自己对待恋爱的忠诚度、你的真实心理年龄、你有依赖心理吗、自卑心理诊断量表等,欢迎联系我们   技术说明: 1.基础是学生资料管理系统,可以独立出来,甚至改造成学生成绩管理等其他系统 2.表格全部用百分比,自动适应800*600、1024*768、1600*1200等分辨率 3.学生网页外观由student.css控制,老师网页外观由teacher.css控制 4.ASP代码最大化简洁,只保留完成功能所必须的部分,全部才几百K 5.支持ACCESS和SQL2000数据库,默认用ACCESS,在data目录 6.数据库连接分别是 student/conn.asp,teacher/conn.asp,teacher/conn2.asp 7.表结构 8.老师和学生的密码默认用MD5加密,请根据自己需要修改 9.默认学生用户222,密码123456;默认老师用户111,密码123456 10.支持将结果直接导出为word或excel格式,需要将网址添加到信任区域 11.支持放在网站根目录或2、3级目录,其余未测试 12.专业、班级、学生都可以进入系统后逐个添加,但是比较慢,建议自己直接批量导入数据库 13.版权所有,免费使用、修改和传播,但请注明出处       相关阅读 同类推荐:站长常用源码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值