Showing posts with label oracle. Show all posts
Showing posts with label oracle. Show all posts

Tuesday, July 17, 2012

How to access Weblogic Server Datasource from jsp

Recently i was working on a project and I faced some security issues regarding the connection string. I was using the DriverManager.getConnection method>

Connection con=DriverManager.getConnection("jdbc:oracle:thin:tom/tom@l91c214gx:1521:orcl");
Later I searched and found a way to handle that. I accessed the Weblogic Server Datasource. Pulled the connection information from the datasource and made a connection to the database. :)


You can find the code below. Change the jdbc/wb_datasource to your datasource name. Always prefix JNDI Name with jdbc like jdbc/wb_datasource
eg: pool = (DataSource) env.lookup ("jdbc/xxxx");



<%@ page import="java.sql.*" %>
<%@ page import="java.util.Hashtable"%>
<%@ page import="javax.naming.*"%>
<%@ page import="javax.sql.DataSource"%>
<%@ page import="java.util.*"%>


<%try{

 Context env = null;
    DataSource pool = null;
    Hashtable ht = new Hashtable( );

    ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
    ht.put(Context.PROVIDER_URL,"t3://localhost:7001");
    
    env = new InitialContext(ht);

    //Lookup this DataSouce at the top level of the WebLogic JNDI tree
    pool = (DataSource) env.lookup ("jdbc/wb_datasource"); 

 //get a java.sql.Connection from the pool
    Connection conn = pool.getConnection( );

 // change the sql_retrive string according to your need
 String sql_retrive = "select * from test" ;



 Statement st1=conn.createStatement();
 st1.execute(sql_retrive);

 ResultSet rs=st1.getResultSet();
 
 ResultSetMetaData rsm = rs.getMetaData( );
 
 int colCount =  rsm.getColumnCount( );

 %> 
 <table><tr>
 <%
  //print column names
        for (int i = 1; i <=colCount; ++i) { %>
            <th><%=rsm.getColumnName(i)%> </th>
 <%  } %>
           </tr>
          <% while( rs.next( )){ %>
                <tr>
    <%for (int i = 1;  i <=colCount; ++i) { %>
                <td>    <%= rs.getString(i) %> </td>
    <%} //for loop ended%>  
                </tr>
          <%} //while loop ended%>
 </table><% 
}
conn.close();
st1.close();
}
catch(Exception ex){

out.println(ex);

}
%>

(Read more inside ..)

Monday, August 17, 2009

How to insert data into oracle database using java program

Oracle CorporationImage via Wikipedia

Hey friends I am here to help you. In this tutorial we will see how to insert data into oracle data base using java program/application.

Prerequisites:
  1. All configuration must me done to connect java

    Java (programming language)Image via Wikipedia

    application to oracle before starting this task. As described in my previous tutorial 'How to connect java application with oracle database'.
Steps:
  1. First start your oracle database and create a table of your choice. eg - create table student (id number(3), name varchar2(30),class number(2),marks number(3));
  2. Now open IDE and write down the java code for the application which will connect to the oracle database and enter the data to the specific table.
  3. After compilation and execution. You will get data inserted in the database.
Source Code for 'Inserting data into oracle database using java program/application':

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class InsertRecord {

public static void main(String[] args) {

String driver="sun.jdbc.odbc.JdbcOdbcDriver";
String cs="jdbc:odbc:connect_oracle";
String user = "system";
String pwd = "tom";
String sqlstmt="INSERT INTO STUDENT VALUES(1,'Steve',5,70)";
Connection con = null;
Statement st = null;
try
{
Class.forName(driver);
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("Driver loaded");
try
{
con=DriverManager.getConnection(cs,user,pwd);
System.out.println("Connected to the Oracle Database");
st = con.createStatement();//creates a Statement object for sending SQL statements to the database.
int updatecount=st.executeUpdate(sqlstmt);//return either the row count for INSERT, UPDATE or DELETE statements, or 0 for SQL statements that return nothing
System.out.println(updatecount+" row inserted");
}
catch(Exception e)
{
System.out.println(e);
}
try
{
st.close();
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}//main()
}//class()

Output:

Driver loaded
Connected to the Oracle Database
1 record inserted


In my next post I will discuss about how to add interactively multiple records to the oracle database. Till then have a good time.

Reblog this post [with Zemanta]
(Read more inside ..)