表单的内容就如下面这样,其它的省了:
<tr>
<td><span class="style2">选择身份:</span></td>
<td><select size="1" name="select">
<option value="a" selected>管理员</option>
<option value="b">总经理</option>
<option value="c">员工</option>
</select></td>
</tr>
验证页我是用 MyEclipe 弄的,,叫 "servlet".......代码如下:
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class servlet extends HttpServlet {
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取也面数据
String userName = "";
String userPwd = "";
userName = request.getParameter("txtUserName");
userPwd = request.getParameter("txtUserPwd");
System.out.println("1 " + userName + " " + userPwd);
// 进行数据读取判断
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
//pubs为你的数据库的
String user = "sa";
String password = "123";
conn = DriverManager.getConnection(url,user,password);
String sql = "";
//执行查询
sql = "SELECT name, pwd FROM [user]";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
if (rs.next()) {
System.out.println(rs.getString(1) + "@@@@" + rs.getString(2));
if (userName.equals(rs.getString(1).trim()) && userPwd.equals(rs.getString(2).trim())) {
response.sendRedirect("index.jsp");
} else {
response.sendRedirect("error.jsp");
}
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (null != rs) {
rs.close();
}
if (null != ps) {
ps.close();
}
if (null != conn) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
怎么再写另外的代码 使得 可以从表单的 select 获取数据!谢谢!我数据库里面是用 Flag=0\1\2做标记!