Connecting Database with Struts

package assignment.actions;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import assignment.forms.QuizBeann;
* @version      1.0
* @author
public class QuizActionAction extends Action
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ActionErrors errors = new ActionErrors();
ActionForward forward = new ActionForward(); // return value
QuizBeann quizBeann = (QuizBeann) form;
try {
          String qns = quizBeann.getQuestion();
          String ans = quizBeann.getAnswer();
          String op1 = quizBeann.getOption1();
          String op2 = quizBeann.getOption2();
          String op3 = quizBeann.getOption3();
          String op4 = quizBeann.getOption4();
     if(qns != null || ans != null || op1 != null || op2 != null || op3 != null || op4 != null){
                    try{
                         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                         Connection con = DriverManager.getConnection("jdbc:odbc:Quiz");
                         Statement s = con.createStatement();
                         String sql = "INSERT INTO Quiz(Qns,Ans,Option1,Option2,Option3,Option4) VALUES ('"+qns+"','"+ans+"','"+op1+"','"+op2+"','"+op3+"','"+op4+"')";
                         ResultSet rs = s.executeQuery(sql);
                         ResultSetMetaData rsmd = rs.getMetaData();
                         int columnCount = rsmd.getColumnCount();
                         StringBuffer result = new StringBuffer();
                         result.append("<HTML><BODY><TABLE border=\"1\" width=\"100%\">");
                         while(rs.next()){
                              result.append("<TR>");
                              for(int i =1; i <= columnCount; i++){
                                   result.append("<TD>");
                                   result.append(rs.getString(i));
                                   result.append("</TD>");
                              result.append("</TR>");
                         result.append("</TABLE></BODY></HTML>");
                         //((HttpServletResponse) quizBeann).getWriter().write(result.toString());
                    catch(Exception e){
                         System.out.println(e.toString());
                    return mapping.findForward("success");
// do something here     
     else {
          return mapping.findForward("failed");
} catch (Exception e) {
// Report the error using the appropriate name and ID.
errors.add("name", new ActionError("id"));
// If a message is required, save the specified key(s)
// into the request for use by the <struts:errors> tag.
if (!errors.isEmpty()) {
saveErrors(request, errors);
// Forward control to the appropriate 'failure' URI (change name as desired)
//     forward = mapping.findForward("failure");
} else {
// Forward control to the appropriate 'success' URI (change name as desired)
// forward = mapping.findForward("success");
// Finish with
return (forward);
I can't seem to run my page with this action mapping. Any idea what is wrong with the codes?

1. When you post code, use code tags to preserve formatting.
2. The way you are trying to do, Struts is an overkill.
3. Struts has a tag to configure a datasource in struts-config.xml.
4. You should not be generating HTML in an Action class. That is not the reason an action class is provided for.
5. Your problem is the beginning of what happens when you mix too much code. Make better use of classes and separate out functionality that does not belong to the action class. It will make life easier for you.
6. You never close the Connection, Statement or ResultSet. Eventually, it would lead to bigger problems. Close all your resources properly.

Similar Messages

  • Problem in connecting MaxDB with Struts 1.2.

    hi,
    how to connect MaxDB in Struts 1.2. when i am trying to connect, i got an error "Unable to initilize struts ActionServlet due to unexpected exception". what are the modifications must be done in struts-config.xml.

    Hi guys,,
    The CCA no longer shows unsupported error message. I can connect the router with CCA right now, but it shows "Unreachable : Unknown Status". I can't see the front panel of the device and I can't configure anything.
    Is there anyone know how to fix it?
    I am really confused right now.
    Thanks in advance.

  • Connecting Database with Oracle WebDB

    Hi Guys,
    Can somebody please help how I can startup webDB with my database instance. The DAtabase has been created and the Web DB has been installed. both on Solaris.How can I connect the two of them and start it up.
    Thanks

    <BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>Can somebody please help how I can startup webDB with my database instance. The DAtabase has been created and the Web DB has been installed. both on Solaris.How can I connect the two of them and start it up.
    <HR></BLOCKQUOTE>
    Write an rc script to start ONE after the OTHER:
    su - oracle -c "dbstart"
    su - oracle -c "lsnrctl start"
    su - oracle -c "nohup wdblsnr hostname port &"
    Or something like that.
    null

  • Forms10gr2 taking 2 min to connect database with same server

    Hi ,
    i have installed oracle11gr2 & forms10gR2 on same server . But when i am connecting forms to database it is taking 2 Min to connect .
    on server i have two Lan card .
    how to minimize time to connect to database .
    Thanks
    Ravi

    Hi
    have you installed a normal Oracle Client also on your Host? normal Oracle Client
    Did you connect with host:port:sid or with a Oracle Naming Service? through TNS Service
    Can you test tnsping <alias> yes, It is working fine
    Did other user have the same problem? yes
    Did you connect through WAN or LAN connection? LAN (Intranet)
    Can you tell more about you client/database setup?
    Database setup:
    OS: Window 2008 server
    version: 11.1.0
    Client: 11.1.0
    OS: Window 2008 server
    Now I am not able to execute single select query which table contains 6 records and 15 columns it is taking long time I have waited 30 min still no resutls
    only one table is behaving like this remaining is working fine
    Edited by: user9235224 on Oct 6, 2012 7:06 PM

  • Connecting databases with the Appletviewer

    Is there any Code needed to include in .HTML file of Applet for Database Connectivity.
    I m running an Applet, which is not connecting to Database Side.
    Running the Applet gives me an Exception, which is:
    java.security.AccessControlException: access denied
    (java.lang.RuntimePermission accessClassInPackage.sun.jdbc.odbc)
    please help.

    JDBC-ODBC driver need privileges for dll loading.
    Here you can find the solution [http://java.sun.com/developer/onlineTraining/Programming/BasicJava1/dba.html] .
    This is a text fragment
    The stack trace means the applet needs read permission to the encoded (binary) file. Here is the DbaOdbPol policy file with the permission indicated by the stack trace added to it:
      grant {
        permission java.lang.RuntimePermission
           "accessClassInPackage.sun.jdbc.odbc";
        permission java.util.PropertyPermission
           "file.encoding", "read";
    Run the applet again. If you use the above policy file with the Runtime and Property permissions indicated, it works just fine.
      appletviewer -J-Djava.security.policy=DbaOdbPol  DbaOdb.htmlSee also [http://www.ryerson.ca/~dgrimsha/courses/cps530/javaPolicyFile.html] , [http://exampledepot.com/egs/java.security/UsePolicy.html] .

  • I cant connect database with dev2000

    i use identif user : scott
    passwd : tiger
    string connection : nothing (empty)
    i cant connect
    error : no module interface connected.
    pls help me
    give me user/passwd and string connection

    Go to SQLNET Easy Configuration and either alter or add database
    alias. Also take the tnsnames.ora file on your server and copy
    it to your oraclehome\network\admin\ folder. Give feedback if
    this helps

  • How to use JDBC driver (type 4) with struts?????

    Hi! have a nice day!!!!!
    i want to connect database use struts with JDBC driver type 4. i must add <data-source/> to <data-sources></data-sources> tag but i don't know how to use <data-source/> with it's properties. please tell me! thank very much

    thank for reply!!! :D
    my project require to use struts 1.2.8 with ODBC (i think so it's type 1) for connect to database (SQL server). i think so it has two step :
    1. edit file struts-config.xml with <data-sources> tag.
    2. programming in file java (which extends from Action class) connect to database.
    but how to programming in file java??? (i think so must use objects DataSource & Connection)

  • Connecting databeses with WML and JSP pages..!

    I need information about how to connect databases with wap technologies using wml and jsp , I am waiting your resources, codes and helps.. And I would like to remind you , I am beginner about this topic , Only things that I know are Jsp and WML , I have no experience about wap and how to combine wml and jsp , So please be helper while you send your messages , If all your messages contains much detailed and supported complete sources and files , it will more helpfull for me ..
    Thanks in Advance.
    P.S : 1- ) if you want to contact direclty , mail address : [email protected]
    2-) Don't post me complex references' URL , I have already made search on google,yahoo and etc

    Additionally , I would like to learn what I have to need for this project .. Now , I already have winwap wap emulator and Websphere Studio.. Do I need special wap server or something else to test my applications..
    Please , hurry up .. I really need your helps..
    Ergin

  • How to specifiy the provider to be Oracle.ManagedDataAccess.Client when creating a dynamic connection string with EF Code First from Database?

    I am trying to use the relatively new Code First from Database with the newest EF (6.x) and on an Oracle database (11g, but I have installed the newest ODTwithODAC). First of all, it works fine as long as the connection string is inside the App.Config file. But when I try to build it dynamically in the C# code (or rather, statically at the moment) it fails. I have it working with a dynamically built connection string when doing Model from Database though, so I'm at a loss right now.
    First, I have created a second constructor for the context class that takes a string and does base(connectionString). Then I build the connection string via
    OracleConnectionStringBuilder oracleBuilder = new OracleConnectionStringBuilder();
    oracleBuilder.DataSource = "TEST.BLA.COM";
    oracleBuilder.UserID = "ABC";
    oracleBuilder.Password = "abc";
    oracleBuilder.PersistSecurityInfo = true;
    string connection = oracleBuilder.ToStrin();
    Now trying to open an EntityConnection by giving to it this provider-specific connection string (or even the static one from the App.Config) doesn't work; I get "keyword not supported: user id"). Trying it by creating a context and giving this connection string doesn't work either. I'm pretty sure that this is because I didn't specify the provider to use; after all, it should use the Oracle.ManagedDataAccess.Client provider and not an SQL Server based one.
    I then tried to get around this by using an EntityConnectionStringBuilder on top and specifying the provider keyword there, but then I get "keyword not supported: provider" when using it in the context constructor, and "the 'metadata' keyword is always required" when using it with the EntityConnection constructor.
    As I said above: I bet it's the provider that I have to specify somehow, but I don't know how. The code that does work is the following:
    using (var context = new Model())
    context.Database.Connection.Open();
    context.Database.Connection.Close();
    When I read context.Database.Connection.ConnectionString, it is exactly the provider-specific connection string I created above, but I don't know where to specify the provider again. Do you know of any way to do this? Certainly there must be one.
    PS: I have also posted this question on http://stackoverflow.com/questions/27979454/ef-code-first-from-database-with-managed-oracle-data-access-dynamic-connection because it is quite urgent and I'd like to use Code First from Database. Otherwise I'd have to go "back" to using Model from Database again, which is not ideal because we have updatable views where the .edmx-file has to be edited after every reload of the model, while with Code First from DB inserting into the view automatically works.

    I am trying to use the relatively new Code First from Database with the newest EF (6.x) and on an Oracle database (11g, but I have installed the newest ODTwithODAC). First of all, it works fine as long as the connection string is inside the App.Config file. But when I try to build it dynamically in the C# code (or rather, statically at the moment) it fails. I have it working with a dynamically built connection string when doing Model from Database though, so I'm at a loss right now.
    First, I have created a second constructor for the context class that takes a string and does base(connectionString). Then I build the connection string via
    OracleConnectionStringBuilder oracleBuilder = new OracleConnectionStringBuilder();
    oracleBuilder.DataSource = "TEST.BLA.COM";
    oracleBuilder.UserID = "ABC";
    oracleBuilder.Password = "abc";
    oracleBuilder.PersistSecurityInfo = true;
    string connection = oracleBuilder.ToStrin();
    Now trying to open an EntityConnection by giving to it this provider-specific connection string (or even the static one from the App.Config) doesn't work; I get "keyword not supported: user id"). Trying it by creating a context and giving this connection string doesn't work either. I'm pretty sure that this is because I didn't specify the provider to use; after all, it should use the Oracle.ManagedDataAccess.Client provider and not an SQL Server based one.
    I then tried to get around this by using an EntityConnectionStringBuilder on top and specifying the provider keyword there, but then I get "keyword not supported: provider" when using it in the context constructor, and "the 'metadata' keyword is always required" when using it with the EntityConnection constructor.
    As I said above: I bet it's the provider that I have to specify somehow, but I don't know how. The code that does work is the following:
    using (var context = new Model())
    context.Database.Connection.Open();
    context.Database.Connection.Close();
    When I read context.Database.Connection.ConnectionString, it is exactly the provider-specific connection string I created above, but I don't know where to specify the provider again. Do you know of any way to do this? Certainly there must be one.
    PS: I have also posted this question on http://stackoverflow.com/questions/27979454/ef-code-first-from-database-with-managed-oracle-data-access-dynamic-connection because it is quite urgent and I'd like to use Code First from Database. Otherwise I'd have to go "back" to using Model from Database again, which is not ideal because we have updatable views where the .edmx-file has to be edited after every reload of the model, while with Code First from DB inserting into the view automatically works.

  • To connect oracle database with ms access database

    i want to connect oracle database with ms access database
    i have follow the following steps
    1. create ms access database.
    2. create system dsn.
    3. make change in listener.ora.
    4. make change in hs folder.
    5.change the tnsname.ora.
    6. lsnrctl stop and lsnrct start.
    7. create database link in oracle sql*plus.
    8.select the table of ms access
    but i have return the foloowing error.
    ORA-12154: TNS:could not resolve the connect identifier specified
    using window 2000, oracle 10g

    Have a look on ths thread, may it helps ...
    Re: copy access data into oracle with form builder 9i

  • CONNECTING ORACLE 9I OR 10G DATABASE WITH ORACLE 10G FORMS AND REPORTS

    pls tell how to connect oracle 9i or 10g database with oracle developer suite 10g . though the forms are getting connected but not running with error as
    FRM-10142 the HTTP listner is not running on pls start the listner or check your runtime preferences.
    now pls tell how to start listner and how to chage runtime preferences.
    though i have worked with oracle 9i and forms 6i where we used to copy the tnsnames.ora from network/admin of oracle to forms 6i tnsnames.ora.
    thank you, you may be thinking such a long question.....

    sir
    By server i mean the computer where oc4j and backend database are running is one one of the client pc not a dedicated server..., these database and 10g dev suite are installed in one of the client machine,
    all the clients are using same internet explorer as browser.
    no message is being displyed on some of the client machine where these forms are not being uploaded on browser instead it displays as done in status bar without actually showing anything at all...
    sir so far as jinitiator or JRE is concered i have seen on the client machine (where my forms are running on internet explorer) a pop up is displyed telling one activex control is required to run this program and click to install it..
    after clicking it download jinitiator from server and after installing jinitiator my forms are loaded and start functioning...
    what i feel is as i said this machine where oc4j and database is running is one of the client machine so some other clients do not have acess to this machine, so this might be the reason why those clints are not able to run the said forms on the internet explorer.... can it be so... kindly tell..
    thank you for your patience for reading the question

  • Hi guys Pls tell me a way to connect db4 database with jsp and which driver

    hi guys
    Pls tell me a way to connect db4 database with jsp and
    also tell me which driver i have to use
    also tell me how to connect with excel sheets

    take a look at the follwing links. There, you'll find all what you need :
    DB4:
    http://www.oracle.com/database/berkeley-db/je/index.html
    http://www.oracle.com/technology/products/berkeley-db/je/index.html
    http://www.oracle.com/database/berkeley-db/db/index.html
    http://www.oracle.com/database/docs/berkeley-db-je-datasheet.pdf
    Excel:
    http://64.18.163.122/rgagnon/javadetails/java-0516.html
    Hope That Helps

  • Connect to oracle database with php script

    Hello!
    How can I connect to an Oracle database with a PHP script? I read about it at www.php.net:
    "Oracle 8 functions; These functions allow you to access Oracle8 and Oracle7 databases. It uses the Oracle8 Call-Interface (OCI8). You will need the Oracle8 client libraries to use this extension."
    Where can I get these libraries from?
    Who knows more about the issue?
    Thanks a lot for your help!
    Martin
    null

    Hello
    <BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>Originally posted by mheibel([email protected]):
    Hello!
    How can I connect to an Oracle database with a PHP script? I read about it at www.php.net:
    "Oracle 8 functions; These functions allow you to access Oracle8 and Oracle7 databases. It uses the Oracle8 Call-Interface (OCI8). You will need the Oracle8 client libraries to use this extension."
    Where can I get these libraries from?
    Who knows more about the issue?
    Thanks a lot for your help!
    Martin<HR></BLOCKQUOTE>
    Install and put working a sqlnet working client (install the Oracle Client option of the Oracle Database CD ROM). Than compile php with the --oci option. Than try the samples in php.net (oci function in the help).
    These are the steps... Put everything working is a little more complicated...
    FS
    null

  • Connecting SAP with an independent SQL database

    Dear all,
    I need to connect SAP with an independent MSSQL database.
    My requirement is to transfer a set of records from my Ztable to a table in SQL database.
    I have maintained the settings in DBCO transaction. I maintained DBMS as 'MSS' in DBCO.
    send some sample code of how to establish the connection. Is it possible to write data in to SQL database.
    How to edit the file tnsnames.ora in application server directories. I heard that for connecting to an oracle database we need to edit this , is it necessary to edit this tnsnames.ora even for connecting with SQL database?
    Thanks in advance.
    Lakshman

    This is not possible.
    My suggestion is that you create shell scripts on the unix side that copy the data to the other database.
    ARD runs it's own private copy of PostGgreSQL inside the package so it won't interfer with any other SQL databases on the network or even on the machine.

  • Two Connection Pools In One Database With Two Phisical Schema

    I have a database with 2 physical schemas. Each physical schema has a different username and password. So i must create a second connection pool. But i have the problem that physical schemas don't understand which connection pool to use. How can i coincide each physical schema to its connection pool automatically.
    Regards

    You need two Physical databases each with its own Connection Pool, within that database will be a single Physical Schema.
    Or you could have a single Database/Connection Pool if it has read access to both schemas.

Maybe you are looking for

  • Logic mastering in 5.1

    Does logic (7.1) have any plug ins that are useful for mastering in 5.1? and if not are there any that are available from other companies? Does the Waves 360 bundle work on logic? as I'm sure its only for TDM on Pro tools? any help is much appreciate

  • WHy can I no longer open multiple tabs; whenever I try each tab is opened but no page is downloaded but no error messages are displayed

    Each tab displays the usual rotational 'downloading' icon but even if I wait several minutes nothing happens..... Even if I halt all or close all tabs bar one I am no longer able to download any pages unless I close Firefox completely and restart. Up

  • Using 6052E card to measure current

    Hi, I am very new to implementing hardware with basic measurement in labview. I have a PXI6052E card. I believe it has AO of -+10 V. Does anyone know where or how I can find examples of how to make a basic multimeter with the card? I want to provide

  • How do you show hidden files

    I'm trying to copy ACL files from msword to get my autocorrect files installed and was given instructions which dont follw it- as all full the address train is not visible, thought if I could open them it would help?

  • [SOLVED] libido3 preventing pacman update exists in filesystem

    After running pacman -Syu, I receive the infamous .../exists in filesystem I read the wiki and pacman -Qo on each of the trouble files results in the owner being libido3 12.10.2-100 I do have unity installed through the arch repositories. If there wa