这是我的程序:
String action = request.getParameter("action");
//System.out.println(action);
if ("certain".equals(action)) {
String username = request.getParameter("username");
System.out.println(username);
String password = request.getParameter("password");
System.out.println(password);
String sql = "select password from bs_users where username = '" + username + "'";
//String sql = "select sysdate from dual";
System.out.println(sql);
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@10.11.12.75:1521:xe";
Connection conn = null;
try {
conn = DriverManager.getConnection(url, "legend", "19840601");
System.out.println("deng lu cheng gong!");
} catch (SQLException e) {
System.err.println("login database failure!");
}
Statement stmt = null;
try {
if (conn != null) { stmt = conn.createStatement();
System.out.println("produce result cheng gong !");
} else
System.err.println("produce result occur error!");
} catch (SQLException e) {
System.err.println(e.getMessage());
System.err.println("bu dui");
}
ResultSet rs = null;
try {
if (stmt != null) { rs = stmt.executeQuery(sql);
System.out.println("cha xun jie guo cheng gong !");
} else
System.err.println("cuo wu");
} catch (SQLException e) {
System.err.println(e.getMessage());
System.err.println("cha xun cuo wu");
}
try {
if (rs != null) {
System.out.println("return result!"); while (rs.next()) {
String ps = rs.getString("password"); System.out.println(ps);
if (password.equals(ps)) {
JOptionPane.showMessageDialog(null, "login success", "welcome to legend bookshop", JOptionPane.INFORMATION_MESSAGE);
System.out.println("welcome to legend bookshop!");
} else
JOptionPane.showMessageDialog(null, "it's error your password", "login failure", JOptionPane.ERROR_MESSAGE);
System.out.println("login password error!");
}
} else {
System.err.println("cha xun shi bai");
} } catch (SQLException e) {
System.err.println("it is failure to link database");
//JOptionPane.showMessageDialog(null, "link databae failure", "error message", JOptionPane.ERROR_MESSAGE);
} finally {
try {
if (stmt != null) {
stmt.close();
conn.close();
}
} catch (SQLException e) {
System.out.println("query closing failure!");
}
} }
catch
(ClassNotFoundException
e
) {
System.err.println("can not link the database");
//JOptionPane.showMessageDialog(null, "fail to connect database", "error message", JOptionPane.ERROR_MESSAGE);
} } else if ("cancel".equals(action)) {
response.sendRedirect("index.jsp");
}
String action = request.getParameter("action");
//System.out.println(action);
if ("certain".equals(action)) {
String username = request.getParameter("username");
System.out.println(username);
String password = request.getParameter("password");
System.out.println(password);
String sql = "select password from bs_users where username = '" + username + "'";
//String sql = "select sysdate from dual";
System.out.println(sql);
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@10.11.12.75:1521:xe";
Connection conn = null;
try {
conn = DriverManager.getConnection(url, "legend", "19840601");
System.out.println("deng lu cheng gong!");
} catch (SQLException e) {
System.err.println("login database failure!");
}
Statement stmt = null;
try {
if (conn != null) { stmt = conn.createStatement();
System.out.println("produce result cheng gong !");
} else
System.err.println("produce result occur error!");
} catch (SQLException e) {
System.err.println(e.getMessage());
System.err.println("bu dui");
}
ResultSet rs = null;
try {
if (stmt != null) { rs = stmt.executeQuery(sql);
System.out.println("cha xun jie guo cheng gong !");
} else
System.err.println("cuo wu");
} catch (SQLException e) {
System.err.println(e.getMessage());
System.err.println("cha xun cuo wu");
}
try {
if (rs != null) {
System.out.println("return result!"); while (rs.next()) {
String ps = rs.getString("password"); System.out.println(ps);
if (password.equals(ps)) {
JOptionPane.showMessageDialog(null, "login success", "welcome to legend bookshop", JOptionPane.INFORMATION_MESSAGE);
System.out.println("welcome to legend bookshop!");
} else
JOptionPane.showMessageDialog(null, "it's error your password", "login failure", JOptionPane.ERROR_MESSAGE);
System.out.println("login password error!");
}
} else {
System.err.println("cha xun shi bai");
} } catch (SQLException e) {
System.err.println("it is failure to link database");
//JOptionPane.showMessageDialog(null, "link databae failure", "error message", JOptionPane.ERROR_MESSAGE);
} finally {
try {
if (stmt != null) {
stmt.close();
conn.close();
}
} catch (SQLException e) {
System.out.println("query closing failure!");
}
} }
catch
(ClassNotFoundException
e
) {
System.err.println("can not link the database");
//JOptionPane.showMessageDialog(null, "fail to connect database", "error message", JOptionPane.ERROR_MESSAGE);
} } else if ("cancel".equals(action)) {
response.sendRedirect("index.jsp");
}
你不是有很多print嗎?核對一下
ps:楼主你这个提问的方式不对,建议你看看提问的智慧
(password);}的时候并没有输出password后面的都没有执行.
把那后面的try catch去掉看报什么错>/
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@10.11.12.75:1521:xe";
String sql = "insert into db_users values(?,?,?,?,?)";
try {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, "legend", "19840601");
} catch (SQLException e) {
System.err.println("deng lu shi bai!");
}
PreparedStatement pst = null;
if (conn != null) {
pst = conn.prepareStatement(sql);
try {
if (pst != null) {
System.out.println("there is result!");
pst.setString(1, username);
pst.setString(2, password);
pst.setString(3, mail);
pst.setString(4, location);
pst.setString(5, sex);
System.out.println("add sql data success!");
if (pst.execute())
System.out.println("add users success!");
else
System.out.println("failure to add user!");
}
} catch (SQLException e) {
System.out.println("produce result failure!");
}
}
} catch (SQLException e) {
System.err.println("login database failure!");
}
} catch (ClassNotFoundException e) {
System.err.println("driver class can not find !");
}
我执行的时候会出现如下提示:
there is result!
add sql data success!
failure to add user!
但是我检查数据库已经有数据存放再里面了.
execute
boolean execute() throws SQLException在此 PreparedStatement 对象中执行 SQL 语句,该语句可以是任何种类的 SQL 语句。一些特别处理过的语句返回多个结果,execute 方法处理这些复杂的语句,executeQuery 和 executeUpdate 处理形式更简单的语句。
execute 方法返回一个 boolean 值,以指示第一个结果的形式。必须调用 getResultSet 或 getUpdateCount 方法来检索结果,并且必须调用 getMoreResults 移动到任何后面的结果。 返回:
如果第一个结果是 ResultSet 对象,则返回 true;如果第一个结果是更新计数或者没有结果,则返回 false
抛出:
SQLException - 如果发生数据库访问错误或者为此方法提供一个参数
另请参见:
Statement.execute(java.lang.String), Statement.getResultSet(), Statement.getUpdateCount(), Statement.getMoreResults()
executeUpdate
int executeUpdate() throws SQLException在此 PreparedStatement 对象中执行 SQL 语句,该语句必须是一个 SQL INSERT、UPDATE 或 DELETE 语句;或者是一个什么都不返回的 SQL 语句,比如 DDL 语句。 返回:
1、对于 INSERT、UPDATE 或 DELETE 语句,返回行数 2、或者对于什么都不返回的 SQL 语句,返回 0
抛出:
SQLException - 如果发生数据库访问错误或者 SQL 语句返回一个 ResultSet 对象
String ps = rs.getString("password"); System.out.println(ps);
应该能打印出ps的结果。还有确保你数据库有password这个字段。
legend
19840601
select password from bs_users where username = 'legend'
deng lu cheng gong!
produce result cheng gong !
cha xun jie guo cheng gong !
return result!