I'm kind of new to struts, but I have this working.

Your data source configuration in struts-config.xml looks fine.  You can check
your logs to make sure no errors are thrown when you start your server (I use
Tomcat 3.2.1).

In your code you set up datasource, as below:

javax.sql.DataSource dataSource = null;
dataSource = servlet.findDataSource(null);

I do it, like so:

 DataSource dataSource = (DataSource)
 servlet.getServletContext().getAttribute(Action.DATA_SOURCE_KEY);

Hope this helps,
Donnie Hall



|--------+----------------------->
|        |          suresh@tufan.|
|        |          com          |
|        |                       |
|        |          03/27/2001   |
|        |          05:15 AM     |
|        |          Please       |
|        |          respond to   |
|        |          struts-user  |
|        |                       |
|--------+----------------------->
  >----------------------------------------------------------------------------|
  |                                                                            |
  |       To:     [EMAIL PROTECTED], [EMAIL PROTECTED]|
  |       cc:     (bcc: Donnie Hall/Enron Communications)                      |
  |       Subject:     Connection to Oracle 8i datasource                      |
  >----------------------------------------------------------------------------|



Hello,
We at Tufan Infotech are evaluating Struts for using in our web application
development.
I am not able to connect to a Oracle 8i database and retrieve or update
data.  The datasource configuration in struts-config.xml is given below.

 <!-- ========== Data Source Configuration ===============================
-->

  <data-sources>
    <data-source
       autoCommit="false"
      description="Oracle Data Source Configuration"
      driverClass="oracle.jdbc.driver.OracleDriver"
         maxCount="4"
         minCount="2"
         password="pisdba123"
              url="jdbc:oracle:thin:@pavan:1521:navin"
             user="pisdba"
    />
  </data-sources>
<!--============================================================== -->

The code snippet which tried to connect is given below

Connection con = null;
PreparedStatement ps = null;
ResultSet rst = null;
javax.sql.DataSource dataSource = null;
String str = "insert into metadata values (?,?,?,?,?)";

String type=((MetadataForm)form).getType();
String code=((MetadataForm)form).getCode();
String description=((MetadataForm)form).getDescription();
String sequence=((MetadataForm)form).getSequence();
String isDefault=((MetadataForm)form).getIsDefault();

ActionErrors errors = new ActionErrors();

          try
          {
               dataSource = servlet.findDataSource(null);
               con = dataSource.getConnection();
               ps = con.prepareStatement(str);
               ps.setString(1,type);
               ps.setString(2,code);
               ps.setString(3,description);
               ps.setString(4,sequence);
               ps.setString(5,isDefault);
               ps.executeUpdate();
          }
          catch(Exception e)
          {
               errors.add(ActionErrors.GLOBAL_ERROR, new ActionError
("error.database.missing"));
          }
          finally
          {
               try
               {
                    con.close();
               }
               catch(Exception e)
               {
                    errors.add(ActionErrors.GLOBAL_ERROR, new ActionError
("error.database.missing"));
               }
          }


The above code is not updating the database.  It thows an exception and
shows the message in 'errors'.  One of the statements below seems to be
failing and gives a nullPointerException.

Would appreciate some inputs to solve the problem.  Also would appreciate
code snippets used to connect to datasources (example Oracle).

Best regards
Suresh





Reply via email to