为了方便我的书写,我的代码密码为password,请你原谅
<%@ page language="java" contentType="text/htmlcharset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=UTF-8">
<title>登陆</title>
<script type="text/javascript">
function checkInput() {
var oUsername = document.loginForm.username.value
if(oUsername == "") {
alert("没有输入姓名!")
oUsername.select()
return false
}
var oUsercode = document.loginForm.password.value
if(oPassword == "") {
alert("请输入密码!")
oPassword.select()
return false
}
return true
}
</script>
</head>
<body>
<form name="loginForm" action="doLogin.jsp" method="post" onsubmit="return checkInput()">
用户名:<input name="username" type="text" /><br/><br/>
密码:<input name="password" type="password" /><br/><br/>
<input type="submit" value="登陆" />
</form>
</body>
</html>
第二步是dologin.jsp进行服务端验证,然后调用dao层。dao层是调用数据库里的内容的
<%@page import="com.dao.UserDao"%>
<%@ page language="java" contentType="text/htmlcharset=UTF-8"
pageEncoding="UTF-8"%>
<%
//处理post请求参数的乱码
request.setCharacterEncoding("utf-8")
//接收用户输入的用户和密码
String strUsername = request.getParameter("username")
String strPassword = request.getParameter("password")
//服务端验证
if(strUsername == null || "".equals(strUsername.trim())) {
out.println("<script>alert('用户名不能为空')window.location.href='login.jsp'</script>")
return
}
if(strPassword == null || "".equals(strPassword.trim())) {
out.println("<script>alert('密码不能为空!')window.location.href='login.jsp'</script>")
return
}
//用户通过服务端的jsp验证后,然后调用服务端的数据库来进行比对。判断用户的输入的信息是否正确
//调用dao层方法
UserDao userDao = new UserDao()
boolean result = userDao.checkLogin(strUsername, strPassword)
if (result) {
response.sendRedirect("index.jsp")
} else {
out.println("<script>alert('登陆失败')window.location.href='login.jsp'</script>")
}
%>
第三步是dao层了,dao层有连接数据和关闭数据库
package com.dao
import java.sql.Connection
import java.sql.DriverManager
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.SQLException
import java.util.ResourceBundle
//连接数据库和关闭数据库的资源的功能
public class BaseDao {
private ResourceBundle rb = ResourceBundle.getBundle("db")
/**
* 获得数据库的连接
* @return
*/
public Connection getConn() {
Connection conn = null
try {
Class.forName(rb.getString("driver"))
conn = DriverManager.getConnection(rb.getString("url"), rb.getString("dbname"), rb.getString("dbpass"))
} catch (ClassNotFoundException e) {
e.printStackTrace()
}catch (SQLException e) {
e.printStackTrace()
}
return conn
}
/**
* 关闭资源
*/
public void closeAll(ResultSet rs, PreparedStatement pstmt, Connection conn) {
if(rs != null) {
try {
rs.close()
} catch (SQLException e) {
e.printStackTrace()
}
}
if(pstmt != null) {
try {
pstmt.close()
} catch (SQLException e) {
e.printStackTrace()
}
}
try {
if(conn != null &&!conn.isClosed()) {
}
} catch (SQLException e) {
e.printStackTrace()
}
}
}
第四步是UserDao的java文件
package com.dao
import java.sql.Connection
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.util.ArrayList
import java.util.List
import com.entity.User
public class UserDao extends BaseDao{
/**
* 根据用户名和密码验证成功登陆或失败
* param username
* param password
* return 登陆成功 true 失败false
*/
public boolean checkLogin(String username, String password) {
boolean result = false
Connection conn = getConn()
PreparedStatement pstmt = null
ResultSet rs = null
try {
conn = getConn()
String sql = "select * from emp where username=? and password=?"
pstmt = conn.prepareStatement(sql)
pstmt.setString(1, username)
pstmt.setString(2, password)
rs = pstmt.executeQuery()
if(rs.next()) {
result = true
}
} catch (Exception e) {
e.printStackTrace()
} finally {
closeAll(rs, pstmt, conn)
/*rs.close()
pstmt.close()
conn.close()*/
}
return result
}
}
第五步是编辑db.properties文件
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
dbname=scott
dbpass=tiger
测试的电脑已经装好Oracle客户端,而且用SQLplus可以连接上。/*
* This sample shows how to list all the names from the EMP table
*
* It uses the JDBC THIN driver. See the same program in the
* oci8 samples directory to see how to use the other drivers.
*/
// You need to import the java.sql package to use JDBC
import java.sql.*
class Test
{
public static void main (String args [])
throws SQLException
{
// Load the Oracle JDBC driver
DriverManager.registerDriver(new oracle.jdbc.OracleDriver())
/* try{
Class.forName("oracle.jdbc.driver.OracleDriver")
}catch(Exception e){
System.out.println("No Driver!")
}
*/
// Connect to the database
// You must put a database name after the @ sign in the connection URL.
// You can use either the fully specified SQL*net syntax or a short cut
// syntax as <host>:<port>:<sid>. The example uses the short cut syntax.
String url = "jdbc:oracle:thin:@172.28.31.85:1521:YIKATONG"
String userName = "scott"
String password = "tiger"
if (args.length >0) url = args[0]
if (args.length >1) userName = args[1]
if (args.length >2) password = args[2]
System.out.println(url)
System.out.println(userName)
System.out.println(password)
Connection conn =
DriverManager.getConnection (url, userName, password)
// Create a Statement
Statement stmt = conn.createStatement ()
// Select the ENAME column from the EMP table
ResultSet rset = stmt.executeQuery ("select * from Test")
// Iterate through the result and print the employee names
while (rset.next ())
System.out.println (rset.getString (1))
}
}
补充日期: 2005-03-14 20:20:29
Java与Oracle的两种连接方式
src=>(作者:huihoo)
第一种方式:通过数据库本身的JDBC Driver连接到数据库
Classs.forName("oracle.jdbc.driver.OracleDriver")
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.33:1521:huihoo","scott","tiger")
第二种方式:通过JDBC-ODBC桥连接到数据库
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
Connection conn = DriverManager.getConnection("jdbc:odbc:192.168.1.33","scott","tiger")
192.168.1.33为数据源
完整的用户登录
Properties props = new Properties()
props.put("user", "scott")
props.put("password", "tiger")
Driver myDriver = (Driver) Class.forName("oracle.jdbc.driver.OracleDriver").newInstance()
conn = myDriver.connect("jdbc:oracle:thin:@192.168.1.33:1521:huihoo", props)
conn.close()
System.out.println("成功登录.")
System.out.println("欢迎您 "+props.getProperty("user")+"!")
尝试获取数据,如果报错,特别是报连接断开的错误,则表明已经断开router.get('/', function (req, res, next) {var oracledb = require('oracledb')
oracledb.getConnection(
{
user: 'username',
password: 'password',
connectString: '192.168.20.10:1521/ORCL'
},function (err, connection) {if (err) {
console.error(err.message) return
}
connection.execute("SELECT * from CMS_FIlE where content_id=:id",
[1072], // bind value for :id
function (err, result) {if (err) {
console.error(err.message) return
}
res.render('index', {title: '查询信息:' + JSON.stringify(result.rows)})
})
})
})