从 MySQL 归档 Jframe
Posted
技术标签:
【中文标题】从 MySQL 归档 Jframe【英文标题】:Filing a Jframe from MySQL 【发布时间】:2012-11-12 13:33:41 【问题描述】:我的项目真的很忙,而且我对 java 还很陌生,对数据库也很陌生。我在代码中所做的是用一堆单词填充“数据”对象,但我想做的是用我制作的数据库中的信息填充这些框架。我一直在谷歌搜索和搜索几天,但没有找到任何可以帮助我理解我需要做什么的东西。任何帮助将不胜感激。
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
import java.awt.event.*;
public class Project3 extends JFrame
private JPanel contentPane;
private JTable table_1;
private JScrollPane scrollPane;
String[] columnNames = "Restaurant", "Dish", "Type", "Price", "Rating" ;
Object[][] data =
"Nemo", "Vesuvio", "Pizza", new String("65kr"), new Integer(7) ,
"John", "Doe", "Rowing", new Integer(3), new Boolean(true) ,
"Sue", "Black", "Knitting", new Integer(2), new Boolean(false) ,
"Jane", "White", "Speed reading", new Integer(20),
new Boolean(true) ,
"Joe", "Brown", "Pool", new Integer(10), new Boolean(false) ;
/**
* Launch the application.
*/
public static void main(String[] args)
EventQueue.invokeLater(new Runnable()
public void run()
try
Project3 frame = new Project3();
frame.setVisible(true);
catch (Exception e)
e.printStackTrace();
);
/**
* Create the frame.
*/
public Project3()
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 600);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(null);
scrollPane = new JScrollPane();
scrollPane.setBounds(139, 162, 469, 420);
panel.add(scrollPane);
table_1 = new JTable(data, columnNames);
scrollPane.setViewportView(table_1);
table_1.setFillsViewportHeight(true);
table_1.setModel(new DefaultTableModel(new Object[][]
"Nemo", "blah", "Pizza", "65kr", new Integer(7) ,
"Nemo", "blah", "Rowing", "1 000 000kr", Boolean.TRUE ,
"Sue", "blah", "Knitting", new Integer(2), Boolean.FALSE ,
"Jane", "blah", "Speed reading", new Integer(20),
Boolean.TRUE ,
"Joe", "Brown", "Pool", new Integer(10), Boolean.FALSE ,
null, null, null, null, null ,
null, null, null, null, null ,
null, null, null, null, null ,
null, null, null, null, null ,
null, null, null, null, null ,
null, null, null, null, null ,
null, null, null, null, null ,
null, null, null, null, null ,
null, null, null, null, null ,
null, null, null, null, null ,
null, null, null, null, null ,
null, null, null, null, null ,
null, null, null, null, null ,
null, null, null, null, null ,
null, null, null, null, null ,
null, null, null, null, null ,
null, null, null, null, null ,
null, null, null, null, null ,
null, null, null, null, null ,
null, null, null, null, null ,
null, null, null, null, null ,
null, null, null, null, null , , new String[]
"Restaurant", "Dish", "Type", "Price", "Rating" ));
JButton btnChangeadd = new JButton("Change/Add");
btnChangeadd.addMouseListener(new MouseAdapter()
@Override
public void mouseClicked(MouseEvent e)
// CALL CHANGE METHOD HERE
);
btnChangeadd.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
btnChangeadd.setBounds(143, 104, 175, 42);
panel.add(btnChangeadd);
JLabel lblNewLabel = new JLabel("Welcome RestaurantID");
lblNewLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 24));
lblNewLabel.setBounds(264, 56, 260, 36);
panel.add(lblNewLabel);
JButton button = new JButton("+");
button.addMouseListener(new MouseAdapter()
@Override
public void mouseClicked(MouseEvent e)
// CALL SOMETHING HERE IF YOU WANT
);
button.setBounds(66, 248, 40, 29);
panel.add(button);
JButton button_1 = new JButton("-");
button_1.addMouseListener(new MouseAdapter()
@Override
public void mouseClicked(MouseEvent e)
);
button_1.setBounds(66, 289, 40, 29);
panel.add(button_1);
JButton btnDelete = new JButton("Delete");
btnDelete.addMouseListener(new MouseAdapter()
@Override
public void mouseClicked(MouseEvent e)
//DELETE METHOD
);
btnDelete.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
btnDelete.setBounds(344, 104, 191, 42);
panel.add(btnDelete);
JButton btnLogOut = new JButton("Log out");
btnLogOut.addMouseListener(new MouseAdapter()
@Override
public void mouseClicked(MouseEvent e)
);
btnLogOut.setBounds(667, 6, 117, 29);
panel.add(btnLogOut);
【问题讨论】:
你的问题是什么,你卡在哪里了? 看到问题是我自己填写了数据并且是数据库的一个完整的新手我无法弄清楚我需要在这个字段中输入什么才能让它拉从数据库中提取,而不必手动将其归档 【参考方案1】:从JDBC tutorial开始,然后研究文章Table From Database。还有,
在可用时使用常量或工厂,而不是不必要的构造函数,例如:
"Nemo", "Vesuvio", "Pizza", "65kr", 7,
"John", "Doe", "Rowing", 3, true,
"Sue", "Black", "Knitting", Integer.valueOf(3), Boolean.FALSE,
使用@Override
注解避免不小心重载:
@Override
public void run()
...
使用Java Collections Framework 开始接受面向对象,而不是数组。请注意引用的示例如何指定形式参数,例如 List<T>
,其中 List
是接口类型,T
是泛型类型参数。
【讨论】:
我还会添加对象化数据(从数据库中生成数据的对象表示),而不是依赖数组。它使生活变得如此轻松,尤其是在处理表格时,因为每个对象都变成了一行 - 恕我直言 @MadProgrammer:好建议;谢谢。看看我是否抓住了你建议的精髓;编辑 ad lib.以上是关于从 MySQL 归档 Jframe的主要内容,如果未能解决你的问题,请参考以下文章