Archive for February, 2007

Using JDBC with Java (Cheapest web hosting) Applications and Applets 94

Monday, February 5th, 2007

Using JDBC with Java Applications and Applets 94 int i = statement.executeUpdate(”INSERT INTO acc_acc VALUES(” + accountIDText.getText() + “, ” + “‘” + usernameText.getText() + “‘, ” + “‘” + passwordText.getText() + “‘, ” + “0″ + “, ” + “now())”); errorText.append(”Inserted ” + i + ” rows successfully”); accountNumberList.removeAll(); loadAccounts(); } catch(SQLException insertException) { displaySQLErrors(insertException); } } } ); JPanel first = new JPanel(new GridLayout(3,1)); first.add(accountNumberListScrollPane); first.add(getAccountButton); first.add(insertAccountButton); accountIDText = new JTextField(15); usernameText = new JTextField(15); passwordText = new JTextField(15); tsText = new JTextField(15); activeTSText = new JTextField(15); errorText = new JTextArea(5, 15); errorText.setEditable(false); JPanel second = new JPanel(); second.setLayout(new GridLayout(6,1)); second.add(accountIDText); second.add(usernameText); second.add(passwordText); second.add(tsText); second.add(activeTSText); JPanel third = new JPanel(); third.add(new JScrollPane(errorText)); c.add(first); c.add(second); c.add(third); setSize(500,500); show(); } Listing 5.3 Our application for inserting a new row. (continues)
Note: If you are looking for top 10 and very good webhost to host and run your jsp application check Actions jsp hosting services

Executing a Query (Subdomains web hosting) with No Results 93 rs.close();

Sunday, February 4th, 2007

Executing a Query with No Results 93 rs.close(); } catch(SQLException e) { displaySQLErrors(e); } accountNumberList.setListData(v); } private void buildGUI() { Container c = getContentPane(); c.setLayout(new FlowLayout()); accountNumberList = new JList(); loadAccounts(); accountNumberList.setVisibleRowCount(2); JScrollPane accountNumberListScrollPane = new JScrollPane(accountNumberList); //Do Get Account Button getAccountButton = new JButton(”Get Account”); getAccountButton.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { try { Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery( “SELECT * FROM acc_acc WHERE acc_id = ” + accountNumberList.getSelectedValue()); if (rs.next()) { accountIDText.setText(rs.getString(”acc_id”)); usernameText.setText(rs.getString(”username”)); passwordText.setText(rs.getString(”password”)); tsText.setText(rs.getString(”ts”)); activeTSText.setText(rs.getString(”act_ts”)); } } catch(SQLException selectException) { displaySQLErrors(selectException); } } } ); //Do Insert Account Button insertAccountButton = new JButton(”Insert Account”); insertAccountButton.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { try { Statement statement = connection.createStatement(); Listing 5.3 Our application for inserting a new row. (continues)

Note: If you are looking for good and affordable webspace to host and run your servlet application check Sandzak servlet hosting services

Using JDBC with Java Applications and Applets 92 (Web hosting and file sharing)

Saturday, February 3rd, 2007

Using JDBC with Java Applications and Applets 92 Let s start with the insert query statement. As we already know, the insert command will allow a new row to be put into a database table. We want to expand our GUI program to allow the user to place an account number, username, and password in the appropriate text boxes and click a button to add the information to the table. Listing 5.3 shows the new code. In addition to the insert button, we have expanded the code to put SQL errors into a JTextArea. import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.sql.*; import java.util.*; public class Accounts extends JFrame { private JButton getAccountButton, insertAccountButton; private JList accountNumberList; private Connection connection; private JTextField accountIDText, usernameText, passwordText, tsText, activeTSText; private JTextArea errorText; public Accounts() { try { Class.forName(”com.mysql.jdbc.Driver”).newInstance(); } catch (Exception e) { System.err.println(”Unable to find and load driver”); System.exit(1); } } private void loadAccounts() { Vector v = new Vector(); try { Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery( “SELECT acc_id FROM acc_acc”); while(rs.next()) { v.addElement(rs.getString(”acc_id”)); } Listing 5.3 Our application for inserting a new row. (continues)
Note: If you are looking for best hosting provider to host and run your tomcat application check Astra tomcat hosting services

Web hosting plans - Executing a Query with No Results 91 Once

Thursday, February 1st, 2007

Executing a Query with No Results 91 Once all of the controls have been created and attached to the application, the frame is sized and displayed to the user. At this point, the user can select an account number on the JList control and click on the Get Account button to display the information on the GUI. Figure 5.4 shows an example of what the output will look like when this is performed. Figure 5.4 Displaying a full record. Executing a Query with No Results Up to now, we have been concentrating on pulling information from the database using a SELECT command. Connector/J, SQL, and MySQL also allow information to be inserted and updated as needed. The operations of insert, delete, and update are considered no-result queries because they don t return a ResultSet object after being executed. For this reason, we don t use the executeQuery() method but instead use a method called executeUpdate(). The signature for the method is int executeUpdate(String SQL); The method accepts a single String parameter, which represents the query to be executed. The query shouldn t cause the database server to return a ResultSet, so no SELECTs are allowed. As you can see, the method will return an integer value after the query is performed. This integer represents the total number of rows affected by the query. The question arises, though, about the actual query statements that do not return a ResultSet. There are quite a few; let s look at the following ones: insert Puts a new row into the database table. delete Removes a row from the database table. update Updates an existing row in the table. drop table Removes a complete table from the database. create table Builds a new table. alter table Changes aspects of the table.
Note: If you are looking for cheap and inexpensive provider to host and run your tomcat application check professional tomcat hosting services