当前位置: 技术问答>java相关
关于servlet查询数据库的问题。
来源: 互联网 发布时间:2015-01-31
本文导语: import java.sql.*; import java.io.*; import javax.servlet.http.*; import javax.servlet.*; public class login extends javax.servlet.http.HttpServlet { Connection con; PreparedStatement pstm; public void init(ServletConfig config)throws ServletException { ...
import java.sql.*;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class login extends javax.servlet.http.HttpServlet
{
Connection con;
PreparedStatement pstm;
public void init(ServletConfig config)throws ServletException
{
super.init(config);
try
{
Driver dirver=(Driver)Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@202.98.46.45:1521:oracle8";
con=DriverManager.getConnection(url,"SCOTT","tiger");
pstm=con.prepareStatement("select * from new_user where user_name=?");
}
catch(Exception e)
{throw(new UnavailableException(this,"Sorry dadabase did't load!"));}
}
public void service(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException
{
ServletOutputStream out=resp.getOutputStream();
String str1=req.getParameter("USERNAME");
String str2=req.getParameter("PASSWORD");
try
{
synchronized(this)
{
pstm.setString(1,str1);
ResultSet res=pstm.executeQuery();
if(res.getString("user_name")==null)
{
out.print("user not exitst!");
}
else if(res.getString("user_password")!=str2)
{
out.print("password not right!");
}
else{out.println("you are login!");}
pstm.close();
con.close();
}}
catch(SQLException e)
{out.println("somethingSQL is wrong!");}
}
}
我建了两个字段user_name,user_password,程序总是抛出SQLException
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class login extends javax.servlet.http.HttpServlet
{
Connection con;
PreparedStatement pstm;
public void init(ServletConfig config)throws ServletException
{
super.init(config);
try
{
Driver dirver=(Driver)Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@202.98.46.45:1521:oracle8";
con=DriverManager.getConnection(url,"SCOTT","tiger");
pstm=con.prepareStatement("select * from new_user where user_name=?");
}
catch(Exception e)
{throw(new UnavailableException(this,"Sorry dadabase did't load!"));}
}
public void service(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException
{
ServletOutputStream out=resp.getOutputStream();
String str1=req.getParameter("USERNAME");
String str2=req.getParameter("PASSWORD");
try
{
synchronized(this)
{
pstm.setString(1,str1);
ResultSet res=pstm.executeQuery();
if(res.getString("user_name")==null)
{
out.print("user not exitst!");
}
else if(res.getString("user_password")!=str2)
{
out.print("password not right!");
}
else{out.println("you are login!");}
pstm.close();
con.close();
}}
catch(SQLException e)
{out.println("somethingSQL is wrong!");}
}
}
我建了两个字段user_name,user_password,程序总是抛出SQLException
|
你再抛出异常的时候有点问题,第一次抛出的时候应该只抛出连接异常,第二次才是抛出查询,但你把查询的连接写在了第一次抛出里面了,
|
取过来的str1没有引号,这样这个sql语句就不对了。
pstm=con.prepareStatement("select * from new_user where user_name="?"");
试试,我也不知道是不是这个原因。
|
应该是这样吧:
pstm=con.prepareStatement("select * from new_user where user_name='?'");
pstm=con.prepareStatement("select * from new_user where user_name='?'");