Access the Oracle database in JSP

Source: Internet
Author: User


Write the first JSP program test. jsp to connect to Oracle
1. Connect to SQL * Plus
Log On As A system/manager User,
SQL> conn system/manager
Create a new user, such as user1/pass1, and grant connect and 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, "1 ");
SQL> insert into test values (2, "2 ");
SQL> insert into test values (3, "3 ");
SQL> commit;
SQL> select * from test;
A B
--------------------
1
2
3 3
2. Configure ODBC (set on the server)
Start-> Settings-> Control Panel-> Administrative Tools-> data source (ODBC)-> system DSN-> Add->
Select "Oracle ODBC Driver"-> finish->
Data Source Name: The Name of test can be obtained at will, just remember it, for example, test
Service Name: oradb Oracle database instance Name. In this example, oradb. Generally, Oracle is installed as ORCL by default.
UserID: user1: Oracle user name, in this example: user1
Press "OK"-> press "OK" to exit

3. Create test. jsp under C: \ JBuilder4 \ tomcat \ webapps \ test, edit it with notebook, and enter the following code
<! -- First import some necessary packages -->
<% @ Page info = "database handler" %>
<% @ Page import = "java. io. *" %>
<% @ Page import = "java. util. *" %>
<! -- Tell the compiler to use the 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;
// Load the driver, the following code is to load 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 connection in ASP
Con = DriverManager. getConnection (url, "user1", "pass1 ");
// Create a JDBC Statement
Stmt = con. createStatement ();
// Add a new record
Stmt.exe cuteUpdate ("insert into test (a, B) VALUES (1," 2 ")");
// Query records
Rs = stmt.exe cuteQuery ("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 displayed data
Out. println ("<tr> <td>" + col1 + "</td> <td>" + col2 + "</td> </tr> ");
}
Out. println ("</table> ");
}
// If an error occurs during loading, an error message is displayed.
Catch (Exception e ){}
%>
4. Open a browser window and enter the following address to view the running result.
Http: // localhost: 8080/test. jsp

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.