开发者

connecting JDBC

Hi guys:) I am new to servlet.I dont know how to connect oracle database to the se开发者_高级运维rvlet application. Can anyone paste Oracle JDBC coding


Register Database Connection parameters: To include your application specific connection parameters of your database, edit the file data-sources.xml under \config directory. Add the following lines to create a data source. Change the hostname, port, username/password, database name, driver type to suit your application. Make changes for url after @ symbol. Save the file. This will register the data-source which can be used across your application.

<data-source 
  class="oracle.jdbc.pool.OracleConnectionPoolDataSource" 
  name="ifso817DS" 
  location="jdbc/Loneifso817DS" 
  xa-location="jdbc/xa/ifso817XADS" 
  ejb-location="jdbc/Pooledifso817DS" 
  url="jdbc:oracle:thin:@insn104a.idc.oracle.com:1521:ifso817" 
  connection-driver="oracle.jdbc.driver.OracleDriver" 
  username="travel" 
  password="travel" 
  inactivity-timeout="30" 
/> 

That is taken from: http://www.oracle.com/technology/sample_code/tech/java/servlets/samples/TravelServlet/Readme.html


There are a lot of articles discussing this, but I'll give you the basic steps:

  1. download Oracle JDBC driver from Oracle's homepage, it's called ojdbc6.jar if you're using Java 6
  2. make sure your application can find it on its classpath
  3. since you are using servlets, you're also using a Java application server; read relevant documentation about setting up a JNDI binding to your database (usually this binding will be called jdbc/connectionName)
  4. inside of your application, use this code:

Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("jdbc/connectionName");
Connection conn = ds.getConnection();

This should be enough to get you started.

For more information, Google is your friend. Check out Wikipedia's JDBC page. Google "Oracle JDBC connection" for more info.

One thing that might give you headaches if you've never done stuff like this: when defining Oracle's URL for your app. server, it's format is as follows:

jdbc:oracle:thin:[user/password]@[host][:port]:SID

So you have to substitute appropriate values when defining the connection for a JNDI binding.


check this http://www.java2s.com/Code/Java/Servlets/JDBCandServlet.htm. You need to modify driver class name and connection url to connect to Oracle DB. Check http://www.java2s.com/Code/Java/Database-SQL-JDBC/OracleJDBCDriverload.htm for Oracle DB connection details

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜