当前位置: 技术问答>java相关
一个数据提交以后和库内数据比对验证的问题。
来源: 互联网 发布时间:2015-10-05
本文导语: default.htm 用户名: ...
default.htm
用户名:
用户名:
密 码:
login.jsp
pass
false
DbConnection.java
ackage db;
import java.util.*;
import java.sql.*;
import java.io.*;
public class DbConnection
{
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;
public DbConnection()
{
}
public boolean openConnection()
{
Properties prop = new Properties();
try{
String filename = "db/db.properties";
FileInputStream is = new FileInputStream(filename);
//InputStream is = getClass().getResourceAsStream("/db.properties");
prop.load(is);
if (is != null) is.close();
}catch(IOException e){
System.out.println("[DbConnection] 打开文件时出现错误");
}
String jdbc = prop.getProperty("drivers");
String url = prop.getProperty("url");
String user = prop.getProperty("user");
String password = prop.getProperty("password");
System.out.println("jdbc=[" + jdbc + "]");
System.out.println("url=[" + url + "]");
System.out.println("user=[" + user + "]");
System.out.println("password=[" + password + "]");
try{
Class.forName(jdbc);
}catch(ClassNotFoundException e){
System.out.println("JDBC 登录过程中出现错误" + e.getMessage());
return false;
}
try{
this.conn = DriverManager.getConnection(url,user,password);
}catch(SQLException e){
System.out.println("生成Connection过程中出现错误:" + e.getMessage());
return false;
}
return true;
}
public ResultSet executeQuery(String query) throws SQLException
{
this.stmt = conn.createStatement();
this.rset = stmt.executeQuery(query);
return rset;
}
public void executeUpdate(String query) throws SQLException
{
this.stmt = conn.createStatement();
this.executeUpdate(query);
if(stmt!=null) stmt.close();
}
public void close() throws SQLException
{
if(conn!=null) conn.close();
if(rset!=null) rset.close();
if(stmt!=null) stmt.close();
}
protected void finalize() throws Throwable
{
this.close();
}
}
ViewCheckQueryBean.java
package db;
import java.sql.*;
public class ViewCheckQueryBean
{
db.DbConnection dc = null;
ResultSet rset = null;
public ViewCheckQueryBean()
{
dc = new db.DbConnection();
}
public boolean openConnection()
{
return dc.openConnection();
}
//***************************************
//SELECT
//***************************************
public void executeQuery(String query) throws SQLException
{
this.rset = dc.executeQuery(query);
}
//********************************
//检测数据是否匹配
//********************************
public boolean checkDate(String query) throws SQLException
{
executeQuery(query);
if (this.rset!=null){
return true;
}
else{
return false;
}
}
}
db.properties
drivers=sun.jdbc.odbc.JdbcDdbcDriver
url=jdbc:odbc:bbs
user=pz
password=
提交以后出现的错误是:
A Servlet Exception Has Occurred
java.lang.NullPointerException
at java.io.Reader.(Reader.java:61)
at java.io.InputStreamReader.(InputStreamReader.java:80)
at java.util.Properties.load(Properties.java:189)
at db.DbConnection.openConnection(DbConnection.java:23)
at db.ViewCheckQueryBean.openConnection(ViewCheckQueryBean.java:17)
at org.apache.jsp.login$jsp._jspService(login$jsp.java:88)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:1264)
at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:201)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:381)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:473)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:1264)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:215)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2366)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:462)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:163)
at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1005)
at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1098)
at java.lang.Thread.run(Thread.java:536)
谁能帮我指出一下错误,谢了。
|
Some problem has happend on my input method, so I only can type Eglish here. I wish I can express exactly.
I read your code carefully, but I found no problem in your beans. I only doubt the string you typed as follows:
String query = "SELECT M_NAME,M_PASSWORD FROM FORUM_MEMBERS WHERE M_NAME="+request.getParameter("username")+" AND M_PASSWORD="+request.getParameter("password");
I think it should looks like this:
String query = "SELECT M_NAME,M_PASSWORD FROM FORUM_MEMBERS WHERE M_NAME='"+request.getParameter("username")+"' AND M_PASSWORD='"+request.getParameter("password") + "'", but i don't know where I am right because my DBMS is oracle, and yours is SQLServer.
still others, the error infomation you supplied above tells me that the problem happened when your application try to read the propertiy file, so you should verify if your file is located in the exact location.
I'm afraid I can not give you more suggestion.
I read your code carefully, but I found no problem in your beans. I only doubt the string you typed as follows:
String query = "SELECT M_NAME,M_PASSWORD FROM FORUM_MEMBERS WHERE M_NAME="+request.getParameter("username")+" AND M_PASSWORD="+request.getParameter("password");
I think it should looks like this:
String query = "SELECT M_NAME,M_PASSWORD FROM FORUM_MEMBERS WHERE M_NAME='"+request.getParameter("username")+"' AND M_PASSWORD='"+request.getParameter("password") + "'", but i don't know where I am right because my DBMS is oracle, and yours is SQLServer.
still others, the error infomation you supplied above tells me that the problem happened when your application try to read the propertiy file, so you should verify if your file is located in the exact location.
I'm afraid I can not give you more suggestion.
|
数据库openConnection错了,错误信息不是告诉你了吗?