package getservlets;import java.io.IOException;
import java.io.PrintWriter;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;
import java.sql.SQLException;import java.sql.Statement;import javax.servlet.*;
import javax.servlet.http.*;public class GetServlets extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>GetServlets</title></head>");
out.println("<body>");
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
//1.装载驱动
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
//System.out.println("driver not found");
}
//2.创建连接. try {
conn=DriverManager.getConnection("jdbc:oracle:thin@localhost:1521:ORCL","scott","woshihk");
} catch (SQLException e) {
} //3.创建发送SQL语句的对象:Statement. try {
stmt=conn.createStatement();
} catch (SQLException e) {
}
//4.进行数据库操作:
try {
rs = stmt.executeQuery("SELECT*FROM dept");
} catch (SQLException e) {
}
//5.处理操作结果
try {
while(rs.next()){
out.println(rs.getInt(1)+" "+ rs.getString(2)+ " " +rs.getString(3)+"<br>");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException sqle) {
// TODO: Add catch code
sqle.printStackTrace();
} }
/*} catch (SQLException e) {
}finally{
try{if(rs!=null)
rs.close;
if(stmt!=null)
stmt.close;
if(conn!=null)
conn.close;
}catch(Exception e)
e.printStackTrace();}*/
out.println("<p>The servlet has received a POST or GET. This is the reply.</p>");
out.println("</body></html>");
out.close();
}
}为什么报空指针错误啊?
我只是想查询数据库里的内容
Q307540690,若有大神帮助小的感激不尽,我是初学者
import java.io.PrintWriter;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;
import java.sql.SQLException;import java.sql.Statement;import javax.servlet.*;
import javax.servlet.http.*;public class GetServlets extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>GetServlets</title></head>");
out.println("<body>");
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
//1.装载驱动
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
//System.out.println("driver not found");
}
//2.创建连接. try {
conn=DriverManager.getConnection("jdbc:oracle:thin@localhost:1521:ORCL","scott","woshihk");
} catch (SQLException e) {
} //3.创建发送SQL语句的对象:Statement. try {
stmt=conn.createStatement();
} catch (SQLException e) {
}
//4.进行数据库操作:
try {
rs = stmt.executeQuery("SELECT*FROM dept");
} catch (SQLException e) {
}
//5.处理操作结果
try {
while(rs.next()){
out.println(rs.getInt(1)+" "+ rs.getString(2)+ " " +rs.getString(3)+"<br>");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException sqle) {
// TODO: Add catch code
sqle.printStackTrace();
} }
/*} catch (SQLException e) {
}finally{
try{if(rs!=null)
rs.close;
if(stmt!=null)
stmt.close;
if(conn!=null)
conn.close;
}catch(Exception e)
e.printStackTrace();}*/
out.println("<p>The servlet has received a POST or GET. This is the reply.</p>");
out.println("</body></html>");
out.close();
}
}为什么报空指针错误啊?
我只是想查询数据库里的内容
Q307540690,若有大神帮助小的感激不尽,我是初学者
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货