October 30, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Binding to MySQL in Java Studio Creator

  • June 21, 2005
  • By Dick Wall
  • Send Email »
  • More Articles »

Setting up a MySQL Data Source

After installing MySQL, unpacking and copying the jar file, and creating a user and loading in some test data, it is time to actually use that data from Java Studio Creator.

  • First, start Java Creator Studio.
  • When it has loaded, from the top-left pane, right-click on Data Sources and choose Add Data Source.
  • A dialog will appear. For Server Type, select Add Server Type. Another dialog will appear.
  • Hit the New... button and navigate to the jar file you downloaded and copied earlier (called mysql-connector-java-3.1.8-bin.jar).
  • Next, set the display name to MySQL.
  • For the Driver Class Name, enter com.mysql.jdbc.Driver.
  • Finally, for the URL template, enter jdbc:mysql://#HOSTNAME:3306/#DATABASE.
Note: These same steps should be applied for any new JDBC-compliant database you want to use in Creator. There are already several defined (such as Oracle and DB2), so check those first, but if yours is not there, you can add it. You will obviously need different jar file settings, and will need to look up the Driver Class Name and the JDBC URL used in the documentation for the JDBC driver for that DB. Also, choose a name that describes the database.

Once you have finished entering the details here, click Close.

Next:

  • In the Add Data Source dialog, select the new MySQL Server type.
  • Set the Data Source Name to creatordemo, the Database Name to creatordemo, Host Name to localhost, User ID to creatordemo, and Password to creatordemo. The Database URL should be filled in for you automatically.
  • Click the Test Connection, and you should get confirmation that the connection was successful. If not, doublecheck all of your fields and try again.

Once it works, click Add, and you should see a new Data Sources entry called creator demo. You can expand this to look at the tables defined (you should see three: courses, scores, and students).

The MySQL datasource is defined and ready to go.

Create a Simple Demo Creator App

Using your new Data Source, you can quickly create a new Creator Project that uses it. This will be nothing fancy, just a student selection list and a grid of courses and scores for now. In the next few articles, you will start doing more ambitious projects using third-party libraries and the like, but for now it's enough to see the data flowing out of MySQL.

  • Create a new project and call it something like MySQLCreatorDemo.
  • Drag a Listbox and a Data Table onto the page.
  • From the server navigator, drag and drop the students table over the top of the list box and select Fill the List when the dialog comes up.
  • Now, drag and drop the scores table onto the Data Table.
  • Double-click on the scores rowset at the bottom of the page; then drag the courses table into the top pane of the resulting query editor.
  • Right-click on the student_id line and select Add Query Criteria.
  • Select the Parameter radio button, and click OK
  • Right-click on the courses_id line and select Add Query Criteria.
  • Select the Value radio button (should already be active) and for the value field type courses.id (this joins the tables).
  • Save all, and then switch back to the Page1.jsp tab. Click on the table until the whole thing is selected, and right-click and select Table Layout.
  • Double-click on the scores.course_id and scores.student_id fields in the right pane to remove them. Then, in the left pane, scroll down to the bottom and double-click on courses.course_number and courses.course_name to add them to the grid. Then, select each of these new fields in turn and use the Up button to position them first and second in the table.
  • Optionally, run through the Displayed columns and change the header text for each into something more descriptive than the field name.
  • Click OK to update the table details.

Now, you need to hook up the dropdown list to affect the results of the table.

  • First, right-click on the dropdown list and select the Auto Submit on Change option.
  • Next, double-click on the dropdown list and it will create a new event handler for you to edit. Add the following code into that handler:
  • 
    try {
       dataTable1Model.setObject(1, dropdown1.getValue());
       dataTable1Model.execute();
    } catch (Exception e) {
       log("person change exception", e);
       error("Exception changing person id: " + e);
    } // end try catch
    
  • In the constructor for the page bean, add the following code:
  • 
    // Initialization Code
    try {
       this.studentsRowSet.execute();
       this.studentsRowSet.next();
       dataTable1Model.setObject(1,
          this.studentsRowSet.getObject("ID"));
    } catch (Exception ex) {
       throw new FacesException(ex);
    } // end try catch
    
  • Now, build your project, and assuming there are no errors, run it.
  • You should see a list of Students last names. Selecting one should update the scores you see in the data table.

Conclusion

This article may already be familiar to many of you if you have already experimented with databases other than pointbase that came with Java Studio Creator. If so, I apologize and please know that I will be back on track with more interesting topics in the next article. However, moving on to more involved topics required example data to be in a database other than pointbase for my own comfort (and hopefully yours) and this article has covered the steps in making that possible. Now that I can provide MySQL dump files to populate example data, I can get back to concentrating on the cooler things that Java Studio Creator can do, including using third-party components, creating pages suitable for viewing on mobile devices, and so forth.

About the Author

Dick Wall is a Lead Systems Engineer for NewEnergy Associates, A Siemens Company based in Atlanta, GA that provides energy IT and consulting solutions for decision support and energy operations. He can be reached for comment on this and other matters at dick.wall@newenergyassoc.com, or check out his blog at http://www.voiceoftheresistance.com.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel