在以下代码中,用户在文本区域中输入查询并提交查询。我正在使用NetBeans 8.0.2和Glassfish 4.1运行。
import java.awt.BorderLayout;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class AddressQuery extends JFrame
{
// Database connection
private static final String URL = "jdbc:derby://localhost:1527/AddressBook";
private static final String USERNAME = "java";
private static final String PASSWORD = "java";
private boolean connectedToDatabase = false;
private Connection connection;
private Statement statement;
private ResultSet resultSet;
// GUI components
private JTextArea queryArea;
private JButton submitButton;
private JTextArea displayArea;
public AddressQuery()
{
super("Address Query Entry");
此代码已成功连接到数据库。
try
{
// Connect to database
connection = DriverManager.getConnection(
URL, USERNAME, PASSWORD );
// Create statement to query database
statement = connection.createStatement();
}
catch (SQLException sqlException )
{
// Disconnect from Database and terminate application
System.out.println("Failure to Connect to Database - exit App");
System.exit(1);
}
// Update database connection status
connectedToDatabase =
为用户在文本区域中输入查询正确设置了GUI
// Set up text area for user to enter queries
queryArea = new JTextArea( 3, 100 );
queryArea.setWrapStyleWord( true );
queryArea.setLineWrap( true );
JScrollPane scrollpane = new JScrollPane( queryArea,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
// Set up button for submitting queries
submitButton = new JButton( "Submit Query" );
// Set up placement of entry text area and submit button in GUI
Box boxNorth = Box.createHorizontalBox();
boxNorth.add( scrollpane );
boxNorth.add( submitButton );
add( boxNorth, BorderLayout.NORTH);
// Set up entry of display text area in GUI
displayArea = new JTextArea();
displayArea.setEditable( false );
add( new JScrollPane( displayArea ), BorderLayout.CENTER );
事件监听器,供用户处理提交的查询
// Event listener for submit button
submitButton.addActionListener (
new ActionListener ()
{
public void actionPerformed( ActionEvent event )
{
// Perform a new query
try
{
setQuery( queryArea.getText() );
}
catch (SQLException sqlException )
{
displayArea.append("\nError performing query - clear entry");
queryArea.setText( "" );
}
}
}
);
以下是处理输入查询并打印出更新数据库以进行调试的代码。
当我输入SELECT * FROM ADDRESSES(ADDRESSES是数据库)时,我得到:
查询= SELECT * FROM ADDRESSES
完整的地址表打印输出(意味着查询成功):
ADDRESSID FIRSTNAME LASTNAME EMAIL PHONENUMBER
1 Mike Green demo1@deitel.com 642-5088
2 Mary Brown demo2@deitel.com 555-1234
当我输入UPDATE ADDRESSES SET PHONENUMBER =' 555-5555'在哪里LASTNAME =' GREEN',我得到:
查询=更新地址设置PHONENUMBER =' 555-5555'在哪里LASTNAME =' GREEN' 但是发生了SQL异常,我没有得到打印出数据库内容的代码。案例似乎没有影响查询失败的成功。
// Process query
public void setQuery( String query )
throws SQLException, IllegalStateException
{
// If not connected to database - display message, throw exception
if ( !connectedToDatabase )
{
displayArea.append("\nNot Connected to Database");
throw new IllegalStateException( "Not Connected to Database");
}
// Execute query
System.out.println("Query = " + query); // debug statement
resultSet = statement.executeQuery( query );
//start debug code
ResultSetMetaData metaData = resultSet.getMetaData();
int numberOfColumns = metaData.getColumnCount();
System.out.println("Addresses Table:\n");
for ( int i = 1; i <= numberOfColumns; i++ )
System.out.printf( "%-8s\t", metaData.getColumnName(i));
System.out.println();
while (resultSet.next())
{
for (int i = 1; i<= numberOfColumns; i++)
System.out.printf("%-8s\t",resultSet.getObject(i));
System.out.println();
}
// end debug code
// Indicate successful query
displayArea.append("\nDatabase
}
关闭连接和主要功能的代码如下。