JAVA未报告的异常错误SQLException
packagecom.control;importjava.io.*;importjava.sql.*;importjavax.servlet.*;importjavax...
package com.control;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.model.BookBean;
public class UserLoginServlet extends HttpServlet {
Connection conn;
public void init() {
String driver="com.microsoft.jdbc.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:microsoft.sqlserver://localhost:1433:DatabaseName=网上书店";
String username="admin";
String password="admin";
// con = DriverManager.getConnection(dburl);
try{
Class.forName(driver); //加载数据库驱动程序
//创建数据库连接对象
conn = DriverManager.getConnection(
url,username,password);
}catch(ClassNotFoundException e1){
}catch(SQLException e2){}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session=request.getSession(true);
String u_name = request.getParameter("u_name");
String u_password = request.getParameter("u_password");
String location="";
String sql="SELECT u_password FROM B_user where u_name=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
request.setCharacterEncoding("GB2312");
pstmt.setString(1,u_name);
ResultSet rst = pstmt.executeQuery();
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();
while(rst.next()){
String password=rst.getString("u_password");
求帮忙改 QAQ 展开
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.model.BookBean;
public class UserLoginServlet extends HttpServlet {
Connection conn;
public void init() {
String driver="com.microsoft.jdbc.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:microsoft.sqlserver://localhost:1433:DatabaseName=网上书店";
String username="admin";
String password="admin";
// con = DriverManager.getConnection(dburl);
try{
Class.forName(driver); //加载数据库驱动程序
//创建数据库连接对象
conn = DriverManager.getConnection(
url,username,password);
}catch(ClassNotFoundException e1){
}catch(SQLException e2){}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session=request.getSession(true);
String u_name = request.getParameter("u_name");
String u_password = request.getParameter("u_password");
String location="";
String sql="SELECT u_password FROM B_user where u_name=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
request.setCharacterEncoding("GB2312");
pstmt.setString(1,u_name);
ResultSet rst = pstmt.executeQuery();
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();
while(rst.next()){
String password=rst.getString("u_password");
求帮忙改 QAQ 展开
3个回答
展开全部
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws Exception {
HttpSession session=request.getSession(true);
String u_name = request.getParameter("u_name");
String u_password = request.getParameter("u_password");
String location="";
String sql="SELECT u_password FROM B_user where u_name=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
request.setCharacterEncoding("GB2312");
pstmt.setString(1,u_name);
ResultSet rst = pstmt.executeQuery();
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();
while(rst.next()){
String password=rst.getString("u_password");
throws Exception {
HttpSession session=request.getSession(true);
String u_name = request.getParameter("u_name");
String u_password = request.getParameter("u_password");
String location="";
String sql="SELECT u_password FROM B_user where u_name=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
request.setCharacterEncoding("GB2312");
pstmt.setString(1,u_name);
ResultSet rst = pstmt.executeQuery();
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();
while(rst.next()){
String password=rst.getString("u_password");
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在 string sql=前加try{},即try{
String sql="SELECT u_password FROM B_user where u_name=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
request.setCharacterEncoding("GB2312");
pstmt.setString(1,u_name);
ResultSet rst = pstmt.executeQuery();
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();
while(rst.next()){
String password=rst.getString("u_password");
}
catch{Exceptione}
String sql="SELECT u_password FROM B_user where u_name=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
request.setCharacterEncoding("GB2312");
pstmt.setString(1,u_name);
ResultSet rst = pstmt.executeQuery();
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();
while(rst.next()){
String password=rst.getString("u_password");
}
catch{Exceptione}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
PreparedStatement pstmt = conn.prepareStatement(sql);这一行吧? 在这一行上到最后加try catch
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询