从具有另一个列表的SQL查询创建列表

时间:2016-10-31 23:02:56

标签: sql spring oracle jsp

我在Oracle数据库中有两个表:CLIENTS(CLIENT_ID, CLIENT_NAME, CLIENT_DESC, ID)PROJECTS(PROJECT_ID, PROJECT_NAME, PROJECT_DESC, CLIENT_ID, CLIENT-NAME)。 我需要填充project CLIENT_NAMECLIENT_ID的projectMgmt.jsp(由用户填写的表单)页面的下拉列表。 我正在使用Spring Framework,因此我不想编写代码来访问java servelet页面上的数据库。 有人可以帮助我使用jsp页面代码以及ProjectDAOImplement.javaProjectController.java代码。 我是网络开发的新手,并且无法完成上述任务。

ProjectDAOImplement.java:

package com.teqtron.dao;

import com.teqtron.dto.Project;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
@Component("projectDAO")
        public class ProjectDAOImplement implements ProjectDAO 
        {
          private JdbcTemplate jdbcTemplate;
          @Autowired
          DataSource dataSource;
          public ProjectDAOImplement()
          {
          }
          public ProjectDAOImplement(DataSource dataSource) 
          {
            jdbcTemplate = new JdbcTemplate(dataSource);
          } 
          @Override
          public void saveOrUpdate(Project project) 
          {
            if (project.getPROJECT_ID() != null) 
           {
             // update
             String sql = "UPDATE PROJECTS SET PROJECT_ID=?, PROJECT_NAME=?,          PROJECT_DESC=?, CLIENT_ID=?, CLIENT_NAME=?";
             jdbcTemplate.update(sql, project.getPROJECT_ID(), project.getPROJECT_NAME(), project.getPROJECT_DESC(), 
                                      project.getCLIENT_ID(), project.getCLIENT_NAME());
           } 
           else 
           {
             // insert
             String sql = "INSERT INTO PROJECTS (PROJECT_ID, PROJECT_NAME, PROJECT_DESC, CLIENT_ID, CLIENT_NAME)" + " VALUES (?, ?, ?, ?, ?)";
             jdbcTemplate.update(sql, project.getPROJECT_ID(), project.getPROJECT_NAME(), project.getPROJECT_DESC(), 
                                      project.getCLIENT_ID(), project.getCLIENT_NAME());
           }
          }
         @Override
         public void delete(String PROJECT_ID) 
         {
           String sql = "DELETE FROM PROJECTS WHERE PROJECT_ID=?";
           jdbcTemplate.update(sql, PROJECT_ID);
         }
        //@Override
        public List<Project> list() 
        {
        jdbcTemplate = new JdbcTemplate(dataSource);
        String sql = "SELECT * FROM PROJECTS";
        List<Project> listProject = jdbcTemplate.query(sql, new RowMapper<Project>() 
         {
            //@Override
            public Project mapRow(ResultSet rs, int rowNum) throws SQLException 
             {
                Project aProject = new Project();

                aProject.setPROJECT_ID(rs.getString("PROJECT_ID"));
                aProject.setPROJECT_NAME(rs.getString("PROJECT_NAME"));
                aProject.setPROJECT_DESC(rs.getString("PROJECT_DESC"));
                aProject.setCLIENT_ID(rs.getString("CLIENT_ID"));
                aProject.setCLIENT_NAME(rs.getString("CLIENT_NAME"));
                return aProject;
             }
        });
        return listProject;
    }

    @Override
    public Project get(String PROJECT_ID) 
    {
        String sql = "SELECT * FROM PROJECTS WHERE PROJECT_ID=" + PROJECT_ID;
        return jdbcTemplate.query(sql, new ResultSetExtractor<Project>() 
        {
            //@Override
            public Project extractData(ResultSet rs) throws SQLException, DataAccessException 
             {
                if (rs.next())
                 {
                    Project project = new Project();
                    project.setPROJECT_ID(rs.getString("PROJECT_ID"));
                    project.setPROJECT_NAME(rs.getString("PROJECT_NAME"));
                    project.setPROJECT_DESC(rs.getString("PROJECT_DESC"));
                    project.setCLIENT_ID(rs.getString("CLIENT_ID"));
                    project.setCLIENT_NAME(rs.getString("CLIENT_NAME"));
                    return project;
                 }
                return null;
              }
        });
    }
}

ProjectController:

package com.teqtron.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.teqtron.dao.ProjectDAO;
import com.teqtron.dto.Project;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Qualifier;

@Controller
public class ProjectController
 {

    @Autowired
    @Qualifier("projectDAO")
    ProjectDAO projectDAO;

    @RequestMapping(value="/listProjects")
    public ModelAndView listProject(ModelAndView model) throws IOException
     {
        List<Project> listProject = projectDAO.list();
        model.addObject("listProject", listProject);
        model.setViewName("manage");

        return model;
     }

    @RequestMapping(value = "/newProject", method = RequestMethod.GET)
    public ModelAndView newProject(ModelAndView model)
     {
        Project newProject = new Project();
        model.addObject("project", newProject);
        model.setViewName("projectMgmt");
        return model;
     }

    @RequestMapping(value = "/saveProject", method = RequestMethod.POST)
    public ModelAndView saveProject(@ModelAttribute Project project) 
     {
        projectDAO.saveOrUpdate(project);       
        return new ModelAndView("projectMgmt");
     }

    @RequestMapping(value = "/deleteProject", method = RequestMethod.GET)
    public ModelAndView deleteProject(HttpServletRequest request) 
     {
        //int Client_id = Integer.parseInt(request.getParameter("CLIENT_ID"));
        String Project_id= request.getParameter("PROJECT_ID");
        projectDAO.delete(Project_id);
        return new ModelAndView("projectMgmt");
     }

    @RequestMapping(value = "/editProject", method = RequestMethod.GET)
    public ModelAndView editProject(HttpServletRequest request) 
     {
        //int ID = Integer.parseInt(request.getParameter("ID"));
        String Project_id= request.getParameter("PROJECT_ID");
        Project project = projectDAO.get(Project_id);
        ModelAndView model = new ModelAndView("projectMgmt");
        model.addObject("project", project);

        return model;
     }
}

projectMgmt.jsp:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>New Project</title>
</head> 
<body> 
<div align="center">
        <h1>New/Edit Project</h1>
        <form:form action="/TeqTimeWeb/saveProject" method="post" modelAttribute="project"/>
        <form>
        <table>
            <tr>
                <td>PROJECT_ID:</td>
                <td><input name="PROJECT_ID" /></td>
            </tr>
            <tr>
                <td>PROJECT_NAME:</td>
                <td><input name="PROJECT_NAME" /></td>
            </tr>
            <tr>
                <td>PROJECT_DESC:</td>
                <td><input name="PROJECT_DESC" /></td>
            </tr>
            <tr>
                <td>CLIENT_ID:</td>
                <td><input name="CLIENT_ID" /></td>
            </tr>
            <tr>
                <td>CLIENT_NAME:</td>
                <td><input name="CLIENT_NAME" /></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="Save"></td>
            </tr>
        </table>
        </form>
    </div>
</body>
</html>

0 个答案:

没有答案