利用Jdbc获取oracle数据库里面数据与用户在网页提交的数据进行匹配
一个超级简单的登陆页面 html写的
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>login.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action="helloworld" name="form" method="get">
用户名:<input type="text" name="username"/> <br>
密码啊:<input type="password" name="psd"/> <br>
<input type="submit" value="login"/>
</form>
</body>
</html>
java代码主要分两部分
part1连接数据库部分(oracle)
package com.wenming.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class dbcon {
private static String driver="oracle.jdbc.driver.OracleDriver";
private static String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
private static String name="sys as sysdba";
private static String psd="wenming";
private static PreparedStatement ps=null;
private static Connection con=null;
/**
* 获得连接对象
* @return
* @throws ClassNotFoundException
* @throws SQLException
*/
public static Connection getcon() throws ClassNotFoundException, SQLException{
Class.forName(driver);
con=DriverManager.getConnection(url, name, psd);
return con;
}
/**
* 关闭数据库连接
* @throws SQLException
*/
void close() throws SQLException{
if (con!=null) {
con.close();
con=null;
}
}
}
part2处理部分,该类需要继承httpservlet类
package com.wenming.test;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wenming.dao.dbcon;
public class helloworld extends HttpServlet {
PreparedStatement ps=null;
Connection connection=null;
dbcon dbcon=null;
ResultSet rs=null;
String name=null;
String psd=null;
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @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 doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
dbcon=new dbcon();
PrintWriter out=response.getWriter();
try {
connection=dbcon.getcon();
ps=connection.prepareStatement("select * from users");
rs=ps.executeQuery();
rs.next();
name=rs.getString(2);
psd=rs.getString(3);
System.out.println(name);
System.out.println(psd);
rs.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String username=request.getParameter("username");
String password=request.getParameter("psd");
System.out.println(username);
System.out.println(password);
if (username.equals(name)&&psd.equals(password)) {
out.println("success");
}else {
out.println("fail");
}
}
}