Write the first JSP program that connects Oracle Test.jsp
1, connect into the Sql*plus
Log in as a System/manager user,
Sql> Conn System/manager
Create a new user: such as USER1/PASS1, give Connect,resource permissions.
Sql> Grant Connect,resource to User1 identified by Pass1;
Sql> Conn User1/pass1
Sql> CREATE TABLE Test (a number,b char (10));
sql> INSERT INTO test values (1, "one");
sql> INSERT INTO test values (2, "II");
sql> INSERT INTO test values (3, "three");
Sql> commit;
Sql> select * from test;
A B
---------- ----------
11
22
33
2. Configure ODBC (set on server side)
Start-> Settings-> Control Panel-> Admin Tools-> Data Source (ODBC)-> System dsn-> Add->
Select Oracle ODBC Driver-> Complete->
Data Source name:test name, easy to remember on the line, such as: test
Service name:oradb The instance name of the Oracle database, in this example: ORADB, the general Oracle default installation is ORCL
Userid:user1 Oracle user name, in this case: User1
Press "OK"-> Press "OK" to exit
3, under C:\JBuilder4\tomcat\webapps\test create test.jsp, with notebook edit, enter the following code
<!--first import some of the necessary packages-->
<%@ page info= "Database handler"%>
<%@ page import= "java.io.*"%>
<%@ page import= "java.util.*"%>
<!--tell the compiler to use SQL package-->
<%@ page import= "java.sql.*"%>
<%@ page import= "javax.servlet.*"%>
<%@ page import= "javax.servlet.http.*"%>
<%
Start with try
Try
{
Connection con;
Statement stmt;
ResultSet rs;
Loading the driver, the following code is loading the JDBD-ODBC driver
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
Connect to the database with the appropriate driver, test "is the System DSN name
String url= "Jdbc:odbc:test";
Establish a connection, similar to creating a database join in ASP
Con=drivermanager.getconnection (URL, "user1", "Pass1");
Create a JDBC Declaration
stmt = Con.createstatement ();
Add new record
Stmt.executeupdate ("INSERT into Test (a,b) VALUES (1," 2 ")");
Query records
rs = Stmt.executequery ("SELECT a,b from Test");
Output query Results
Out.println ("<table border=1 width=400>");
while (Rs.next ())
{
String col1 = rs.getstring (1);
String col2 = rs.getstring (2);
Print the data that is displayed
Out.println ("<tr><td>" +col1+ "</td><td>" +col2+ "</td></tr>");
}
Out.println ("</table>");
}
If an error occurs during loading, give the appropriate error message
catch (Exception e) {}
%>
4, open a browser window, enter the following address to view the results of the operation
http://localhost:8080/test/test.jsp
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service