Donate Share

Swing-XML Authoring Tool

Subscribe

JDBC as Datasource

You are viewing a single message from this topic. View all messages.

  1. 2007-11-30 10:54:34 UTC
    Thanks for this guide, I tryed to implement this idea but I have troubles in step 5

    My JDBCDataSource is like this:

    package es.test.swixat.databinding;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import org.apache.commons.jxpath.JXPathContext;
    import org.swixat.SwiXAT;
    import org.swixat.databinding.AbstractDataSource;

    public class JDBCDataSource extends AbstractDataSource
    {
    private String connectionJDBC;
    private String query;

    /** Creates a new instance of JDBCDataSource */
    public JDBCDataSource ()
    {
    super();
    }

    public String getConnectionJDBC ()
    {
    return connectionJDBC;
    }

    public void setConnectionJDBC (String connectionJDBC)
    {
    this.connectionJDBC = connectionJDBC;
    }

    public String getQuery ()
    {
    return query;
    }

    public void setQuery (String query)
    {
    this.query = query;
    }

    protected Object extractData(JXPathContext sharedContext)
    {
    Object connection = SwiXAT.getFactory().getBean(getConnectionJDBC());
    Object query = SwiXAT.getFactory().getBean(getQuery());
    Connection con=null;
    Statement stmt=null;
    ResultSet rs=null;

    try
    {
    Class.forName("com.mysql.jdbc.Driver");
    }
    catch(java.lang.ClassNotFoundException e)
    {
    System.err.print("ClassNotFoundException: ");
    System.err.println(e.getMessage());
    }

    try
    {
    con = DriverManager.getConnection((String)connection, "user", "password");
    stmt = con.createStatement();
    rs = stmt.executeQuery((String)query);
    while(rs.next())
    {
    System.out.println(rs.getString("id")+"\t"+rs.getString("texto"));
    }
    rs.first ();
    }
    catch(SQLException ex)
    {
    System.err.println("SQLException: " + ex.getMessage());
    }
    return getPropertyValue(rs, sharedContext);
    }

    }

    /****************************************************************/

    Bean definition in context.xml

    <bean id="connectionJDBC" class="java.lang.String">
    <constructor-arg><value>jdbc:mysql://192.168.1.201/test</value></constructor-arg>
    </bean>

    <bean id="query" class="java.lang.String">
    <constructor-arg><value>select * from info where id=1</value></constructor-arg>
    </bean>

    <!-- TagLibrary extensions -->
    <bean id="newTags" class="org.swixat.framework.TagLibraryExt">
    <property name="newTags">
    <map>
    <entry key="JDBCDataSource"><value>es.altia.swixat.databinding.JDBCDataSource</value></entry>
    </map>
    </property>
    </bean>

    /***********************************************************/
    IU Defintion:

    <?xml version="1.0" encoding="UTF-8"?>
    <frame size="640,280" title="Hello SWIXML World" defaultCloseOperation="JFrame.EXIT_ON_CLOSE">

    <JDBCDataSource id="JDBCDATA" connectionJDBC="connectionJDBC" query="query" />

    <panel constraints="BorderLayout.CENTER">
    <label labelfor="tf" font="Georgia-BOLD-12" foreground="blue" text="Hello World!"/>
    <textfield id="tf" columns="20" Text="Swixml" origin="$JDBCDATA/texto"/>
    <button text="Click Here" action="submit"/>
    </panel>

    <panel constraints="BorderLayout.SOUTH">
    <label font="Georgia-BOLD-36" text="Clicks:"/>
    <label font="Georgia-BOLD-36" id="cnt"/>
    </panel>

    </frame>

    /*************************************************************************/
    Everything compiles well and runs, the only problem is the value of texto doesn´t appeared. The connection to Mysql is well because the line "System.out.println(rs.getString("id")+"\t"+rs.getString("texto"));" prints the info I want to show in textField.-

    Any suggestion?

    Thanks in advance.
< Previous | 1 | Next >

Add a Reply

This forum does not allow anonymous participation.

Log in to add a reply. Not registered? Create an account to participate and receive email updates when replies are posted to this topic.