Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按姓氏从学生订单中选择*; List students=new ArrayList(); try(Connection con=dataSource.getConnection(); PreparedStatement ps=con.prepareStatement(sql)){ //如有必要,在此处设置参数 //示例:“stmt.setInt(1,id);”表示“其中id=?” try(ResultSet rs=ps.executeQuery()){ while(rs.next()){ int id=rs.getInt(“id”); String firstName=rs.getString(“first_name”); String lastName=rs.getString(“last_name”); String email=rs.getString(“电子邮件”); 添加(新学生(id、名字、姓氏、电子邮件)); } } } 留学生; } }_Java - Fatal编程技术网

Java 按姓氏从学生订单中选择*; List students=new ArrayList(); try(Connection con=dataSource.getConnection(); PreparedStatement ps=con.prepareStatement(sql)){ //如有必要,在此处设置参数 //示例:“stmt.setInt(1,id);”表示“其中id=?” try(ResultSet rs=ps.executeQuery()){ while(rs.next()){ int id=rs.getInt(“id”); String firstName=rs.getString(“first_name”); String lastName=rs.getString(“last_name”); String email=rs.getString(“电子邮件”); 添加(新学生(id、名字、姓氏、电子邮件)); } } } 留学生; } }

Java 按姓氏从学生订单中选择*; List students=new ArrayList(); try(Connection con=dataSource.getConnection(); PreparedStatement ps=con.prepareStatement(sql)){ //如有必要,在此处设置参数 //示例:“stmt.setInt(1,id);”表示“其中id=?” try(ResultSet rs=ps.executeQuery()){ while(rs.next()){ int id=rs.getInt(“id”); String firstName=rs.getString(“first_name”); String lastName=rs.getString(“last_name”); String email=rs.getString(“电子邮件”); 添加(新学生(id、名字、姓氏、电子邮件)); } } } 留学生; } },java,Java,PS:尽量避免使用my和for属性名和变量名的前缀,因为它们不会添加任何信息。保持一致并使用有意义的名称总是好的。这就是我在上面的代码段中更改了一些名称的原因。也许HashSet可以帮到你?使用它而不是列表。你应该定义要使用的hashCode和equals方法龙目山可以帮你轻松完成 package com.luv2code.web.jdbc; import java.io.IOException; import java.util.List; import javax.annotation.

PS:尽量避免使用my和for属性名和变量名的前缀,因为它们不会添加任何信息。保持一致并使用有意义的名称总是好的。这就是我在上面的代码段中更改了一些名称的原因。

也许HashSet可以帮到你?使用它而不是列表。你应该定义要使用的hashCode和equals方法龙目山可以帮你轻松完成
package com.luv2code.web.jdbc;

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

import javax.annotation.Resource;
import javax.servlet.RequestDispatcher;
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 javax.sql.DataSource;

/**
 * Servlet implementation class StudentControllerServlet
 */
@WebServlet("/StudentControllerServlet")
public class StudentControllerServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private StudentDButil studentDbUtil;
    
    @Resource(name="jdbc/web_student_tracker")
    private DataSource dataSource;
    
    @Override
    public void init() throws ServletException {
        super.init();
        
        // create our student db util ... and pass in the conn pool / datasource
        try {
            studentDbUtil = new StudentDButil(dataSource);
        }
        catch (Exception exc) {
            throw new ServletException(exc);
        }
    }
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            listStudents(request,response); 
        } catch(Exception e) {
            throw new ServletException(e);
        }
    }

    private void listStudents(HttpServletRequest request, HttpServletResponse response) 
        throws Exception {

        // get students from db util
        List<Student> students = studentDbUtil.getStudent();
        
        // add students to the request
        request.setAttribute("STUDENT_LIST", students);
                
        // send to JSP page (view)
        RequestDispatcher dispatcher = request.getRequestDispatcher("/list-students.jsp");
        dispatcher.forward(request, response);
    }
}
<%@ page import="java.util.*,com.luv2code.web.jdbc.*" %>
<!DOCTYPE html>

<html>
<head>
Student Tracker App
</head>
<% List<Student> theStudent=(List<Student>)request.getAttribute("STUDENT_LIST"); %>

<body>
    <%= theStudent %>
</body>

</html>
package com.luv2code.web.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

public class StudentDButil {

    List<Student> students = new ArrayList<>();
    
    private DataSource datasources;
    
    public StudentDButil (DataSource thedatasources)
    {
        datasources = thedatasources;
    }
    
    public List<Student> getStudent() throws Exception
    {
        Connection mycon = null;
        Statement mystmt = null;
        ResultSet myres = null;
        try {
            mycon = datasources.getConnection(); //get DB connection
            String sqlStmt = "select * from student order by last_name"; //write an sql statement
            mystmt = mycon.createStatement(); //create sql statement
            myres = mystmt.executeQuery(sqlStmt); //execute query
            while(myres.next())
            {
                //retrive data from resultset
                int id = myres.getInt("id");
                String fname = myres.getString("first_name");
                String lname = myres.getString("last_name");
                String emailid = myres.getString("email");
    
                Student std = new Student(id, fname, lname, emailid); //create student object
                students.add(std); //add student object to list
            }
            return students;
        }
        finally
        {
            close(mycon, mystmt, myres); //close jdbc connections
        }
        
    }
    
    private void close(Connection mycon, Statement mystmt, ResultSet myres) {
        try {
            if(myres!=null)
            {
                myres.close();
            }
            if(mystmt!=null)
            {
                mystmt.close();
            }
            if(mycon!=null) {
                mycon.close(); //dosen't really close it. just puts back into the connection pool
            }
            
        }
        catch(Exception exc)
        {
            exc.printStackTrace();
        }
    }
    
    //get DB connection

}
public class StudentDbUtil {

    private final DataSource dataSource;

    public StudentDbUtil(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public List<Student> getStudent() throws SQLException {
        String sql = "select * from student order by last_name";

        List<Student> students = new ArrayList<>();
        try (Connection con = dataSource.getConnection();
             PreparedStatement ps = con.prepareStatement(sql)) {
            // set parameters here if necessary
            // example: "stmt.setInt(1, id);" for "WHERE id=?"
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    int id = rs.getInt("id");
                    String firstName = rs.getString("first_name");
                    String lastName = rs.getString("last_name");
                    String email = rs.getString("email");
                    students.add(new Student(id, firstName, lastName, email));
                }
            }
        }
        return students;
    }
}