java web Servlet HTML 登陆,用连接sqlserver数据库的名字和密码登录遇到的问题

无敌浪浪 先进数通 java项目工程师  2015-08-23 10:56:57
package com.servlet.test1;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.*;
/**
* Servlet implementation class LoginServlet
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
static{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("数据库驱动程序加载成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("数据库驱动程序加载失败");
}
}

public LoginServlet() {
super();
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);

}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
request.setCharacterEncoding("UTF-8");

String username = request.getParameter("username");
String password = request.getParameter("password");

try {
Connection conn = DriverManager.getConnection(
"jdbc:sqlsServer://127.0.0.1:1433;DataName=UserInfo","sa","hulang");
PreparedStatement ps = conn.prepareStatement(
"select count(*) from Table_1 where username=?");
//这个用户名是用户输入的
ps.setString(1, username);
ResultSet rs = ps.executeQuery();
while(rs.next()){
//这个密码是数据库里面存的密码,然后你拿这个和输入的对比就可以了

String password1 = rs.getString("password");
}
System.out.println("数据库连接成功!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("数据库连接失败!");
}
}
}
...全文
174 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
nicholasbobo 2015-08-24
把你那个sql改成select count(*) from Table_1 where username=? and password=? 当然密码字段的名称你根据实际情况自己写 然后再判断查询出来的条数,等于1就认为用户验证通过,否则验证失败
回复
没看懂你的意思,什么叫用户名是输入的,密码是数据库的
回复
无敌浪浪 2015-08-23
有大大吗 ,求解答
回复
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-08-23 10:56
社区公告
暂无公告