// This program displays the ResultSet returned by a
// query on the Books database.
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class MysqlJava extends JFrame {
// java.sql types needed for database processing
private Connection connection;
private Statement statement;
private ResultSet resultSet;
private ResultSetMetaData rsMetaData;
private String tableName;
// javax.swing types needed for GUI
private JTable table;
private JTextArea inputQuery;
private JButton submitQuery;
private JButton findName, addName,
updateName, clear, help,set_option;
//private SetOption option;
private JPopupMenu pupupMenu;
private JRadioButtonMenuItem items[];
private String str1,query_result;
public MysqlJava()
{
super("数据库接口类");
// SetOption option=new SetOption();
final JPopupMenu popupMenu=new JPopupMenu();//生成弹出菜单
tableName=new String("authorisbn");
// Load the driver to allow connection to the database
try {
Class.forName("org.gjt.mm.mysql.Driver");
connection=DriverManager.getConnection("jdbc:mysql://localhost/books","root","761222");
}
catch ( ClassNotFoundException cnfex ) {
System.err.println(
"Failed to load JDBC/ODBC driver." );
cnfex.printStackTrace();
System.exit( 1 ); // terminate program
}
catch ( SQLException sqlex ) {
System.err.println( "Unable to connect" );
sqlex.printStackTrace();
System.exit( 1 ); // terminate program
}
str1=new String("select * from ");
String query_resutl=new String();
query_result=str1+tableName;
// tableName="select * from "+tableName;
inputQuery =
new JTextArea( query_result,4, 30 );
Container c = getContentPane();
c.setLayout( new BorderLayout() );
c.add( new JScrollPane( inputQuery),
BorderLayout.NORTH);
JPanel topPanel = new JPanel();
topPanel.setLayout( new GridLayout(1,8));
set_option=new JButton("option");
set_option.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
// SetOption option=new SetOption();
//tableName=option.getTableName();
//System.out.println(tableName);
Object[] obj1={"authorisbn","authors","publishers","titles"};
Object selectedValue=JOptionPane.showInputDialog(null,"选择数据表","请选择要操作的数据表",
JOptionPane.INFORMATION_MESSAGE,null,obj1,obj1[0]);
tableName=selectedValue.toString();
query_result=str1+tableName;
Container c = getContentPane();
c.remove(2);
inputQuery.setText(query_result);
inputQuery.repaint();
getTable();
}
}
);
// set_option.addActionListener(new SetOption());
topPanel.add(set_option);
submitQuery = new JButton( "Display" );
topPanel.add(submitQuery);
submitQuery.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
// inputQuery.repaint();
Container c = getContentPane();
c.remove(2);
getTable();
}
}
);
findName = new JButton( "Find" );
// findName.addActionListener( new FindRecord( c, s, t ) );
topPanel.add( findName );
addName = new JButton( "Add" );
// addName.addActionListener( new AddRecord( c, s, t ) );
topPanel.add( addName );
/* addName.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
deleteTable();
}
}
);
*/
updateName = new JButton( "Update" );
// updateName.addActionListener(
// new UpdateRecord( c, s, t ) );
topPanel.add( updateName );
clear = new JButton( "Clear" );
// clear.addActionListener( new ClearFields( s ) );
topPanel.add( clear );
help = new JButton( "Help" );
// help.addActionListener( new Help( t ) );
topPanel.add( help );
c.add( topPanel, BorderLayout.SOUTH);
//菜单操作
//topPanel.add( submitQuery, BorderLayout.SOUTH );
// JMenu fileMenu=new JMenu("file");
/* JMenu fileMenu = new JMenu( "File" );
fileMenu.setMnemonic( 'F' );
JMenuItem aboutItem = new JMenuItem( "About..." );
aboutItem.setMnemonic( 'A' );
aboutItem.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
JOptionPane.showMessageDialog(null,"AA");
}
}
);
fileMenu.add( aboutItem );
JMenuItem exitItem = new JMenuItem( "Exit" );
exitItem.setMnemonic( 'x' );
exitItem.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
System.exit( 0 );
}
}
);
fileMenu.add( exitItem );
JMenuBar bar=new JMenuBar();
setJMenuBar(bar);
bar.add(fileMenu);
topPanel.add(bar);
//c.add(bar);
*/
//弹出式菜单
/* ItemHandler handler=new ItemHandler();
String tables[]={"authorisbn","authors","publisher","titles"};
ButtonGroup tableGroup=new ButtonGroup();
items=new JRadioButtonMenuItem[4];
for (int i=0;i<items.length;i++)
{
items[i]=new JRadioButtonMenuItem(tables[i]);
popupMenu.add(items[i]);
tableGroup.add(items[i]);
items[i].addActionListener(handler);
}
addMouseListener(
new MouseAdapter() {
public void mousePressed( MouseEvent e )
{ checkForTriggerEvent( e ); }
public void mouseReleased( MouseEvent e )
{ checkForTriggerEvent( e ); }
private void checkForTriggerEvent( MouseEvent e )
{
if ( e.isPopupTrigger() )
popupMenu.show( e.getComponent(),
e.getX(), e.getY() );
}
}
);
*/
getTable();
setSize( 400, 400 );
show();
}
/* private class ItemHandler implements ActionListener {
public void actionPerformed( ActionEvent e )
{
// determine which menu item was selected
for ( int i = 0; i < items.length; i++ )
if ( e.getSource() == items[ i ] ) {
tableName=items[i].getText();
// System.out.println(tableName);
// repaint();
// return;
getTable();
}
}
}
private void showMenu(JPopupMenu p)
{
//JOptionPane.showMessageDialog(null,"aa");
p.show();
}
*/
private void deleteTable()
{
try {
//String query = inputQuery.getText();
String query = inputQuery.getText();
//inputQuery.repaint();
statement = connection.createStatement();
int result = statement.executeUpdate(query);
if (result>0)
JOptionPane.showMessageDialog(null,"success");
else
JOptionPane.showMessageDialog(null,"failed");
statement.close();
}
catch
mysql_java_interce.rar_java 数据库_java 查找 数据库
版权申诉
170 浏览量
2022-09-23
22:58:26
上传
评论
收藏 14KB RAR 举报
Kinonoyomeo
- 粉丝: 76
- 资源: 1万+