?? dvdframe.java
字號:
package dvd;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import com.borland.jbcl.layout.*;
public class DVDFrame extends JFrame {
private Connection connection;
private JTable table;
private String query = "SELECT * FROM movie_inf ";
public boolean haverecords;
public int number;
public DVDFrame(Connection Connection)
{
// The URL specifying the Books database to which
// this program connects using JDBC to connect to a
// Microsoft ODBC database.
connection=Connection;
haverecords=false;
// Load the driver to allow connection to the database
getTable();
setSize( 450, 450);
if(haverecords==true)
show();
else this.dispose();
}
public DVDFrame(Connection Connection,String sqlquery)
{
// The URL specifying the Books database to which
// this program connects using JDBC to connect to a
// Microsoft ODBC database.
connection=Connection;
haverecords=false;
// Load the driver to allow connection to the database
query=sqlquery;
getTable();
setSize( 450,450 );
if(haverecords==true)
show();
else this.dispose();
}
private void getTable()
{
Statement statement;
ResultSet resultSet;
try {
statement = connection.createStatement();
resultSet = statement.executeQuery( query );
displayResultSet( resultSet );
statement.close();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}
private void displayResultSet( ResultSet rs )
throws SQLException
{
// position to first record
boolean moreRecords = rs.next();
// If there are no records, display a message
if ( ! moreRecords ) {
haverecords=false;
JOptionPane.showMessageDialog( this, "對不起,本店未存入相關信息,請重新輸入" );
setTitle( "No records to display" );
return;
}
else haverecords=true;
setTitle( "Informations table from DVD" );
Vector columnHeads = new Vector();
Vector rows = new Vector();
try {
// get column heads
ResultSetMetaData rsmd = rs.getMetaData();
for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
columnHeads.addElement( rsmd.getColumnName( i ) );
// get row data
do {
number+=1;
rows.addElement( getNextRow( rs, rsmd ) );
} while ( rs.next() );
// display table with ResultSet contents
table = new JTable( rows, columnHeads );
JScrollPane scroller = new JScrollPane( table );
getContentPane().add(scroller, BorderLayout.CENTER );
validate();
}
catch ( SQLException sqlex ) {
sqlex.printStackTrace();
}
}
private Vector getNextRow( ResultSet rs, ResultSetMetaData rsmd )
throws SQLException
{
Vector currentRow = new Vector();
for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
switch( rsmd.getColumnType( i ) ) {
case Types.VARCHAR:
currentRow.addElement( rs.getString( i ) );
break;
case Types.INTEGER:
currentRow.addElement(
new Long( rs.getLong( i ) ) );
break;
case Types.CHAR:
currentRow.addElement( rs.getString(i));
break;
case Types.TINYINT:
currentRow.addElement(
new Long( rs.getLong( i ) ) );
break;
case Types.SMALLINT:
currentRow.addElement(
new Long( rs.getLong( i ) ) );
break;
default:
System.out.println( "Type was: " +
rsmd.getColumnTypeName( i ) );
}
return currentRow;
}
public void shutDown()
{
try {
connection.close();
}
catch ( SQLException sqlex ) {
System.err.println( "Unable to disconnect" );
sqlex.printStackTrace();
}
}
public DVDFrame() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setFont(new java.awt.Font("Dialog", 0, 15));
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -