1.打开服务
2.把class12.jar文件拷备到web-inf/lib下面
3.端口号:
在SQL/PLUS工具中运行如下可以得到服务名 select global_name from global_name; 至于端口号可以在Net Manager中打开看到 说一点,我昨天刚装了一遍,服务端默认的端口号一般是389,客户端默认的端口号一般都是1521
4.连接代码:(DBManager.java)
------------------------------------------
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBManager {
public static Connection getConnection() throws SQLException {
try {
System.out.println("---------connection-----------");
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
System.out.println("111111111111111111");
e.printStackTrace();
}
System.out.println("123123131");
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl","dbconnect","dbconnect");
return con;
}
public static void clearup(PreparedStatement pstmt) {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException se) {
se.printStackTrace();
}
pstmt = null;
}
}
public static void clearup(Connection conn, PreparedStatement pstmt) {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException se) {
se.printStackTrace();
}
pstmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
conn = null;
}
}
public static void clearup(Connection conn, PreparedStatement pstmt,
ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException se) {
se.printStackTrace();
}
rs = null;
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException se) {
se.printStackTrace();
}
pstmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
conn = null;
}
}
public static void clearup(PreparedStatement pstmt, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException se) {
se.printStackTrace();
}
rs = null;
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException se) {
se.printStackTrace();
}
pstmt = null;
}
}
}
------------------------------------------
5.DbconnectServlet.java
--------------------------------
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
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;
import com.util.DBConnection;
import com.util.DBManager;
public class DbconnectServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.println(username+password);
if(validateUser(username,password))
{
response.sendRedirect("login.jsp");
System.out.println(validateUser(username,password));
}
else
{
response.sendRedirect("error.jsp");
System.out.println(validateUser(username,password));
}
}
private boolean validateUser(String name, String pwd) {
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
System.out.println("===============================aaaa");
pstmt = DBManager.getConnection().prepareStatement(
"select * from login where login_username=? and login_password=?");
pstmt.setString(1, name);
pstmt.setString(2, pwd);
rs = pstmt.executeQuery();
if (rs.next())
return true;
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBManager.clearup(pstmt, rs);
}
return false;
}
}
2.把class12.jar文件拷备到web-inf/lib下面
3.端口号:
在SQL/PLUS工具中运行如下可以得到服务名 select global_name from global_name; 至于端口号可以在Net Manager中打开看到 说一点,我昨天刚装了一遍,服务端默认的端口号一般是389,客户端默认的端口号一般都是1521
4.连接代码:(DBManager.java)
------------------------------------------
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBManager {
public static Connection getConnection() throws SQLException {
try {
System.out.println("---------connection-----------");
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
System.out.println("111111111111111111");
e.printStackTrace();
}
System.out.println("123123131");
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl","dbconnect","dbconnect");
return con;
}
public static void clearup(PreparedStatement pstmt) {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException se) {
se.printStackTrace();
}
pstmt = null;
}
}
public static void clearup(Connection conn, PreparedStatement pstmt) {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException se) {
se.printStackTrace();
}
pstmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
conn = null;
}
}
public static void clearup(Connection conn, PreparedStatement pstmt,
ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException se) {
se.printStackTrace();
}
rs = null;
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException se) {
se.printStackTrace();
}
pstmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
conn = null;
}
}
public static void clearup(PreparedStatement pstmt, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException se) {
se.printStackTrace();
}
rs = null;
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException se) {
se.printStackTrace();
}
pstmt = null;
}
}
}
------------------------------------------
5.DbconnectServlet.java
--------------------------------
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
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;
import com.util.DBConnection;
import com.util.DBManager;
public class DbconnectServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.println(username+password);
if(validateUser(username,password))
{
response.sendRedirect("login.jsp");
System.out.println(validateUser(username,password));
}
else
{
response.sendRedirect("error.jsp");
System.out.println(validateUser(username,password));
}
}
private boolean validateUser(String name, String pwd) {
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
System.out.println("===============================aaaa");
pstmt = DBManager.getConnection().prepareStatement(
"select * from login where login_username=? and login_password=?");
pstmt.setString(1, name);
pstmt.setString(2, pwd);
rs = pstmt.executeQuery();
if (rs.next())
return true;
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBManager.clearup(pstmt, rs);
}
return false;
}
}