zyb3511
发贴: 3
|
于 2006-08-24 09:01
我是个java初学者~我现在要做一个数据库的查询~但就是调试不成功~这些源码很多也是复制别人的.请大家帮我看看问题出在哪里吧~万分感谢 <%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<%@ page import="java.sql.*" %> <% request.getSession(true); // 启用 Session 功能 String USER = request.getParameter("username"); // 取得使用者输入的账号资料 String PASS = request.getParameter("password"); // 取得使用者输入的密码资料 // 以下将宣告与数据库联系的程序 String driverName="com.mysql.jdbc.Driver";
String userName="root";
String userPasswd="123";
String dbName="test";
String tableName="table3";
String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement(); ResultSet rs=null;
//获得数据结果集合
ResultSetMetaData rmeta = rs.getMetaData();
//确定数据集的列数,亦字段数
int numColumns=rmeta.getColumnCount(); %>
<html> <head><title>密码验证</title></head> <body bgcolor="#ccccff"> <% String check = "SELECT name, pass FROM tableName WHERE name= '" +USER + "' AND pass= '" + PASS +"' "; rs=statement.executeQuery(check); String Test1 = null; //初始化 String Test2 = null; // 初始化 // 以下将逐一取出数据库中的使用者账户,密码 if (rs.next()) { Test1 = rs.getString("name"); // 取出数据库中的使用者账号 Test2 = rs.getString("pass"); // 取出数据库中的使用者密码 } %> <% out.println(check); if(USER.equals(Test1) && PASS.equals(Test2)) { // 验证成功,将使用者的账号、密码存放到 Session 物件中 session.setAttribute("NAME",request.getParameter("username")); session.setAttribute("PWD",request.getParameter("password")); rs.close(); statement.close(); connection.close(); response.sendRedirect("2.jsp");} else { rs.close(); statement.close(); connection.close(); response.sendRedirect("error.jsp"); } %> </body> </html>
|