One: JDBC Connection database (Oracle Reference)
public class DBTest {//test
public static void Main (string[] args) {
ResultSet rs = null;
Statement stmt = null;
Connection conn = null;
try {
Load Driver
Class.forName ("Oracle.jdbc.driver.OracleDriver");
Connecting to a database
String url= "JDBC:ORACLE:THIN:@192.168.0.XXX:1521:ORCL";
conn = drivermanager.getconnection (URL, "Ms_test", "1");
SYSTEM.OUT.PRINTLN ("Connection succeeded ...");
stmt = Conn.createstatement ();
rs = Stmt.executequery ("SELECT * from Test");
while (Rs.next ()) {
System.out.println (rs.getstring ("uname"));//Column name
System.out.println (rs.getstring ("pwd"));//Column name
}
} catch (ClassNotFoundException e) {
E.printstacktrace ();
} catch (SQLException e) {
E.printstacktrace ();
} finally {
try {
if (rs! = null) {
Rs.close ();
rs = null;
}
if (stmt! = null) {
Stmt.close ();
stmt = null;
}
IF (conn! = null) {
Conn.close ();
conn = null;
}
} catch (SQLException e) {
E.printstacktrace ();
}
}
}}
If you can get the success!
Note: Ojbc6.jar issues, special attention should be placed in the Lib directory.
Second: JSP Simple Login interface
1: Create a Dynamic Web project
2: New JSP file (login.jsp)
3: Modify the Web. xml file
<!-corresponding login jsp,
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
4: Jump page
1) Successful success.jsp
2) Failure fail.jsp
5: Processing the transaction interface
chuli.jsp
As follows:
LOGIN.JSP encoded by: UTF-8 or GBK
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "Utf-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body>
<form action= "chuli.jsp" method= "POST" >//here to handle the transaction interface, and then determine the success of the jump or failure interface.
<table align= "center" >
<tr>
<td> Account </td>
<td><input type= "text" name= "uname" ></td>
</tr>
<tr>
<td> Password </td>
<td><input type= "password" name= "pwd" ></td>
</tr>
<tr>
<td><input type= "Submit" value= "Login" ></td>
<td><input type= "reset" value= "reset" ></td>
</tr>
</table>
</form></body>
success.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "Utf-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTM1 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body>
Login successful
</body>
fail.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "Utf-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body>
Logon failure
</body>
Core Processing interface
chuli.jsp
<% @page import= "Java.sql.DriverManager"%>
<% @page import= "Java.sql.ResultSet"%>
<% @page import= "Java.sql.PreparedStatement"%>
<% @page import= "Java.sql.Connection"%>
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "Utf-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body>
<!--get login information-
<% String uname=request.getparameter ("uname");
String pwd=request.getparameter ("pwd"); %>
<%
try{
Class.forName ("Oracle.jdbc.driver.OracleDriver");
String url= "JDBC:ORACLE:THIN:@192.168.0.248:1521:ORCL";
Connection conn = drivermanager.getconnection (URL, "Ms_test", "1");
String sql= "SELECT * from Test where Uname=?and pwd=?";
PreparedStatement ps=conn.preparestatement (SQL);
Ps.setstring (1, uname);
Ps.setstring (2, PWD);
ResultSet Re=ps.executequery ();
if (Re.next ()) {
%>
<jsp:forward page= "success.jsp" ></jsp:forward>
<%
}else{
%>
<jsp:forward page= "fail.jsp" ></jsp:forward>
<%
}
}catch (Exception e) {
E.printstacktrace ();
}
%>
</body>
Summary: Driver package Storage directory issues, the driver package can be found in the Oracle installation directory action= "" Processing problems, encoding problems.
Three: JSP simple registration
3 JSP files required
1. Register zhuce.jsp
2. Successful registration success1.jsp
3. Registration failure fail1.jsp
4. Transaction Processing Core chuli2.jsp
As follows:
Register zhuce.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "Utf-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body>
<form action= "chuli2.jsp" method= "POST" >//Core processing transaction JSP
<table align= "center" >
<tr>
<td> Account:</td>
<td><input type= "text" name= "uname" ></td>
</tr>
<tr>
<td> Password:</td>
<td><input type= "password" name= "pwd" ></td>
</tr>
<tr>
<td><input type= "Submit" value= "Registration" ></td>
<td><input type= "reset" value= "reset" ></td>
<td><a href= "login.jsp" > Back to login </a></td>
</tr>
</table>
</form>
</body>
chuli2.jsp
<% @page import= "Java.sql.DriverManager"%>
<% @page import= "Java.sql.ResultSet"%>
<% @page import= "Java.sql.PreparedStatement"%>
<% @page import= "Java.sql.Connection"%>
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "Utf-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body>
<!--get registration information-
<% String uname=request.getparameter ("uname");
String pwd=request.getparameter ("pwd"); %>
<%
try{
Class.forName ("Oracle.jdbc.driver.OracleDriver");
String url= "JDBC:ORACLE:THIN:@192.168.0.248:1521:ORCL";
Connection conn = drivermanager.getconnection (URL, "Ms_test", "1");
String sql= "INSERT into test values (?,?)"; /insert into Information insert Database
PreparedStatement ps=conn.preparestatement (SQL);
Ps.setstring (1, uname);
Ps.setstring (2, PWD);
if (Ps.executeupdate () >-1) {//corresponds to update
%>
<jsp:forward page= "success1.jsp" ></jsp:forward>
<%
}else{
%>
<jsp:forward page= "fail1.jsp" ></jsp:forward>
<%
}
}catch (Exception e) {
E.printstacktrace ();
}
%>
</body>
success1.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "Utf-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTM1 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body>
Registration Success <a href= "login.jsp" > Login </a>//Registration Success Click the login jump login page
</body>
fail1.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "Utf-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body>
Registration failed <a href= "zhuce.jsp" > re-registration </a>
</body>
JSP easy Login and registration and JDBC Connection Oracle