晕,又不可以了。奇怪了!!??## 下面是个servlet: package com.test;import java.io.IOException; import java.io.PrintWriter; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List;import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;public class SplitPage extends HttpServlet { /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ List list = new ArrayList(); User user = null; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String sql="select * from t_user"; ResultSet rs = null; Statement stmt = null; ConnectionDB connection = new ConnectionDB(); try {
package com.test;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class ConnectionDB { private Connection conn;
//ResultSet rs;
private Statement stmt;
public Statement connDB( ) throws SQLException{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:database","system","dbuser");
stmt = conn.createStatement();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stmt;
}
}
带main方法的测试类:package com.test;import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class TestDB { /**
* @param args
*/
static String sql="select * from t_user";
static ResultSet rs;
public static void main(String[] args) {
// TODO Auto-generated method stub
ConnectionDB connection = new ConnectionDB();
try {
Statement stmt = connection.connDB();
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while(rs.next()){
String userName = rs.getString("userName");
System.out.println(userName);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
servlet:
下面是个servlet:
package com.test;import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class SplitPage extends HttpServlet { /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
List list = new ArrayList();
User user = null;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String sql="select * from t_user";
ResultSet rs = null;
Statement stmt = null;
ConnectionDB connection = new ConnectionDB();
try {
stmt = connection.connDB();
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while(rs.next()){
user = new User();
user.setUserName(rs.getString("userName"));
user.setPassword(rs.getString("password"));
user.setAddress(rs.getString("address"));
list.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.setAttribute("users",user);
request.getRequestDispatcher("resultPage.jsp").forward(request,response);
}}