Tutorials to .com

Tutorials to .com » Jsp » Database » JDBC connection with Oracle database (to resolve this problem, and would like to sum up are welcome to point out that lack of brothers)

JDBC connection with Oracle database (to resolve this problem, and would like to sum up are welcome to point out that lack of brothers)

Print View , by: iSee ,Total views: 13 ,Word Count: 1081 ,Date: Sun, 19 Apr 2009 Time: 3:37 AM

Usually have four options to connect, I only used two of which I have used tomcat under win98
WinNT server is the installation of oracle
I like the path is: D: \ tomcat \ webapps \ ROOT \ WEB-INF \ classes \ yourpackage
jsp path: D: \ tomcat \ webapps \ ROOT \
Call path: http://127.0.0.1:8080/connOracle.jsp
(You is not, the path I can try ^ _ ^)
1, the use of bridge JDBC_ODBC
First of all, remember that the data source in the system in the establishment of an ODBC connection can be used ^_^,( Oh PB or other procedures to connect the database to test the success of this establishment is not ODBC)
The next category of documents to write, you can use the following types of documents:
package yourpackage; / / Note, java class storage systems and file system of the oh:)
import java.sql .*;
public class opendb
(
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc: odbc: yourdb"; / / here is your yourdb built the original ODBC data of the DNS
Connection conn = null;
ResultSet rs = null;
public opendb ()
(
try (
Class.forName (sDBDriver);
)
catch (java.lang.ClassNotFoundException e)
(
System.err.println ( "opendb ();"+ e.getMessage ());
)
)
public ResultSet executeQuery (String sql)
(
rs = null;
try (
conn = DriverManager.getConnection (sConnStr, "username", "password"); / / Note database user name and password must be set according to the actual situation
Statement stmt = conn.createStatement ();
rs = stmt.executeQuery (sql);
)
catch (SQLException ex) (
System.err.println ( "aq.executeQuery:" + ex.getMessage ());
)
return rs;
)
)
This method is relatively simple, usually more easily achieved, but there are loopholes reportedly (I did not do research deep-seated), and not suitable for large databases of large-scale data traffic, so try to direct the use of JDBC to connect ^ _ ^

2, the direct use of JDBC to connect to the database the first category of documents to you, take a look at it, is almost the same, that is, setting connection parameters:
package yourpackage;
import java.sql .*;
public class openOracle
(
String sDBDriver = "oracle.jdbc.driver.OracleDriver"; / / Do not think that this drive is the system already, I also added it was before, and the key Oh
String sConnStr = "jdbc: oracle: thin: @ server: 1521: yourservername"; / / should pay attention to you, server for your database with the name of the server is located, the general use Oracle port 1521, of course, you can advice your system administrator about ^ _ ^, yourservername is your database with the service name
Connection conn = null;
ResultSet rs = null;
public openOracle ()
(
try (
Class.forName (sDBDriver);
)
catch (java.lang.ClassNotFoundException e)
(
System.err.println ( "openOracle ();"+ e.getMessage ());
)
)
public ResultSet executeQuery (String sql)
(
rs = null;
try (
conn = DriverManager.getConnection (sConnStr, "username", "password");
Statement stmt = conn.createStatement ();
rs = stmt.executeQuery (sql);
)
catch (SQLException ex) (
System.err.println ( "aq.executeQuery:" + ex.getMessage ());
)
return rs;
)
)
Compile successfully, but the browser implementation, error: (always a lot of systems do not recognize out of a class, when I found that when tomcat startup DOS window pop out of two errors (I captured the catch)
openOracle (): oracle.jdbc.odbc.OracleDriver
aq.executeQuery: No suitable driver
It can be speculated that the driver can not find, through to ask friends and found that the lack of documents from classes12.zip can be found. I search on my D: \ Oracle \ jdbc \ lib to find the document. Decompression through a variety of attempts, as well as analysis of reasoning, I unzip them after the Oracle folder on the D: \ tomcat \ lib under the, run the program, successful, can be exciting to see the "connection successful "The results of the ^ _ ^
The two methods above, you can use the following to test the document, simply modify the corresponding class files call it. ^ _ ^
<% @ Page contentType = "text / html; charset = GBK"%>
<% @ Page language = "java" import = "java.sql .*"%>
<jsp:useBean id="db" scope="page" class="yourpackage.openOracle"/>
<%
String sql = "select * from user"; / / Here, you can not find you a table of simple queries to produce results as long as you can (remember to find a data table, oh: P)
ResultSet rs = db.executeQuery (sql);
String mess = "";
if (rs.next ())
(
mess = "Connection successful!";
)
else
(
mess = "Connection failed!";
)
rs.close ();
%>
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">
<TITLE> Database connection test </ TITLE>
</ HEAD>
<BODY>
<H2>
Database connection test results:
<% = Mess%>
</ H2>
<P> </ P>
</ BODY>
</ HTML>

Give it a try! No problem, the basic procedures, if not, look for various reasons, put the directory on the wrong types of documents need to have a bag? Do not pay attention to low-level errors committed Oh ^ _ ^
Good luck! ^ _ ^


Jsp And Database Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.