19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

}<br />

jbtClearSQLCommand.addActionListener(new ActionListener() {<br />

public void actionPerformed(ActionEvent e) {<br />

jtasqlCommand.setText(null);<br />

}<br />

});<br />

jbtClearSQLResult.addActionListener(new ActionListener() {<br />

public void actionPerformed(ActionEvent e) {<br />

jtaSQLResult.setText(null);<br />

}<br />

});<br />

/** Connect <strong>to</strong> DB */<br />

private void connectToDB() {<br />

// Get database information from the user input<br />

String driver = (String)jcboDriver.getSelectedItem();<br />

String url = (String)jcboURL.getSelectedItem();<br />

String username = jtfUsername.getText().trim();<br />

String password = new String(jpfPassword.getPassword());<br />

}<br />

// Connection <strong>to</strong> the database<br />

try {<br />

connection = DriverManager.getConnection(<br />

url, username, password);<br />

jlblConnectionStatus.setText("Connected <strong>to</strong> " + url);<br />

}<br />

catch (java.lang.Exception ex) {<br />

ex.printStackTrace();<br />

}<br />

/** Execute SQL <strong>com</strong>mands */<br />

private void executeSQL() {<br />

if (connection == null) {<br />

jtaSQLResult.setText("Please connect <strong>to</strong> a database first");<br />

return;<br />

}<br />

else {<br />

String sqlCommands = jtasqlCommand.getText().trim();<br />

String[] <strong>com</strong>mands = sqlCommands.replace('\n', ' ').split(";");<br />

}<br />

}<br />

for (String aCommand: <strong>com</strong>mands) {<br />

if (aCommand.trim().<strong>to</strong>UpperCase().startsWith("SELECT")) {<br />

processSQLSelect(aCommand);<br />

}<br />

else {<br />

processSQLNonSelect(aCommand);<br />

}<br />

}<br />

/** Execute SQL SELECT <strong>com</strong>mands */<br />

private void processSQLSelect(String sqlCommand) {<br />

try {<br />

// Get a new statement for the current connection<br />

statement = connection.createStatement();<br />

// Execute a SELECT SQL <strong>com</strong>mand<br />

ResultSet resultSet = statement.executeQuery(sqlCommand);<br />

5

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!