index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="connection" scope="application" class="com.JDBCConnection" />
<%
String sql="select * from tb01";
java.sql.ResultSet rs=connection.executeQuery(sql);
%>
<select name="user">
<%
try{
while(rs.next()){
%>
<option value="<%=rs.getString("name")%>"><%=rs.getString("name")%></option>
<%
}
}catch(Exception e){}
%>
</select>
</body>
</html>
JDBCConnection.java:
package com;
import java.sql.*;
public class JDBCConnection{ //连接SQL数据库的方法
private final String dbDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private final String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db01";
private final String userName="sa";
private final String password="1536248654";
private Connection conn=null;
public JDBCConnection(){
try{
Class.forName(dbDriver).newInstance();//加载数据库驱动
}catch(Exception ex){
}
}
public boolean creatConnection(){ //创建数据库连接
try{
conn=DriverManager.getConnection(url,userName,password);
}catch(SQLException e){
}
return true;
}
public ResultSet executeQuery(String sql){ //对数据库的查询操作
ResultSet rs;
try{
if(conn==null){
creatConnection();
}
Statement stmt=conn.createStatement();
//PreparedStatement ps= conn.prepareStatement(sql);
try{
rs=stmt.executeQuery(sql);
//rs=ps.executeQuery ();
}catch(SQLException e){
return null;
}
}catch(SQLException e){
return null;
}
return rs;
}
public void closeConnection(){ //关闭数据库的操作
if(conn!=null){
try{
conn.close();
}catch(SQLException e){
}finally{
conn=null;
}
}
}
}
驱动包已经放进去了··为啥还出现一下错误啊:
org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:500)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:428)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
java.lang.NullPointerException
com.JDBCConnection.executeQuery(JDBCConnection.java:28)
org.apache.jsp.index_jsp._jspService(index_jsp.java:75)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
有时还会出现这句话话错误:
java.sql.ResultSet rs=connection.executeQuery(sql);
跪求打人- -跪求!!!!!
------解决方案--------------------