Всем привет!
Очень нужна помощь, а то сам ну никак не могу разобратьса. Причина мучений – создание интерфейса POS терминала с подклучением базы данных. Вот эту- то самую БД у меня и не получаетса подсоединить к остальному коду. На данный момент готов сам интерфейс (main menu) и отделно база данных – подскажите пожалуйста как их объединить, чтоб при заходе в main menu в левом window видеть базу данных, а в правом – key pad. Зарание спасибо всем кто откликнетса.
Kod dla EFT POS frame gde baza dannih dolzhna pojavitsa:
import java. awt. event.*;
import javax. swing.*;
import java. awt.*;
import java. sql.*;
public class Eftpos extends JFrame
{
private KeyPad pad;
private JTextArea display;
static String url = "jdbc: odbc: Product";
public Eftpos()
{
final int FRAME_ WIDTH = 500;
final int FRAME_ HEIGHT = 200;
setSize(FRAME_ WIDTH, FRAME_ HEIGHT);
addWindowListener(new WindowCloser());
// construct components
pad = new KeyPad();
display = new JTextArea(8, 20);
// add components to content pane
Container contentPane = getContentPane();
contentPane. setLayout(new FlowLayout());
contentPane. add(display);
contentPane. add(pad);
}
private class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent event)
{ System. exit(0);}
}
}
Kod dla key EFT POS keypad:
import java. awt. BorderLayout;
import java. awt. GridLayout;
import java. awt. event. ActionEvent;
import java. awt. event. ActionListener;
import javax. swing. JButton;
import javax. swing. JPanel;
import javax. swing. JOptionPane;
import javax. swing. JTextField;
/**
A GUI component that lets the user enter a number, using
a button pad labeled with digits
*/
public class KeyPad extends JPanel
{
private JPanel buttonPanel;
private JButton clearButton;
private JTextField display;
public KeyPad()
{
setLayout(new BorderLayout());
// add display field
display = new JTextField();
add(display, "North");
// make button panel
buttonPanel = new JPanel();
buttonPanel. setLayout(new GridLayout(4, 3));
// add digit buttons
ActionListener listener = new DigitButtonListener();
addButton("7", listener);
addButton("8", listener);
addButton("9", listener);
addButton("4", listener);
addButton("5", listener);
addButton("6", listener);
addButton("1", listener);
addButton("2", listener);
addButton("3", listener);
// add clear entry button
clearButton = new JButton("CE");
buttonPanel. add(clearButton);
clearButton. addActionListener
(new ClearButtonListener());
addButton("0", listener);
addButton("EN", listener);// needs action implementation
add(buttonPanel, "Center");
}
/**
Gets the value that the user entered.
@return the value in the text field of the keypad
*/
public double getValue()
{// modified by JD to issue error dialog
while ( display. getText().equals("") )
{
String UserInput = JOptionPane. showInputDialog
("Please enter the required input value:");
display. setText(UserInput);
}
return Double. parseDouble(display. getText());
}
/**
Clears the dislay.
*/
public void clear()
{ display. setText("");
}
/**
Adds a button to the button panel
@param label the button label
@param listener the button listener
*/
public void addButton(String label,
ActionListener listener)
{ JButton button = new JButton(label);
buttonPanel. add(button);
button. addActionListener(listener);
}
private class DigitButtonListener implements ActionListener
{ public void actionPerformed(ActionEvent event)
{ // Get the button label
// it is a digit or decimal point
JButton source = (JButton)event. getSource();
String label = source. getText();
display. setText(display. getText() + label);
}
}
private class ClearButtonListener implements ActionListener
{ public void actionPerformed(ActionEvent event)
{ clear();
}
}
}
Kod dla bazi dannih:
import java. util.*;
import java. sql.*;
import java. awt.*;
import javax. swing. table.*;
import javax. swing.*;
public class DatabaseTableModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
private ArrayList<String> columnNames = new ArrayList<String>();
private ArrayList<Class> columnTypes = new ArrayList<Class>();
private ArrayList<ArrayList<Object>> data = new ArrayList<ArrayList<Object>>();
public int getRowCount() {
synchronized (data) {
return data. size();
}
}
public int getColumnCount() {
return columnNames. size();
}
public Object getValueAt(int row, int col) {
synchronized (data) {
return data. get(row).get(col);
}
}
public String getColumnName(int col) {
return columnNames. get(col);
}
public Class getColumnClass(int col) {
return columnTypes. get(col);
}
public boolean isCellEditable(int row, int col) {
return true;
}
public void setValueAt(Object obj, int row, int col) {
synchronized (data) {
data. get(row).set(col, obj);
}
}
/**
* Core of the model. Initializes column names, types, data from ResultSet.
*
* @param rs ResultSet from which all information for model is token.
* @throws SQLException
* @throws ClassNotFoundException
*/
public void setDataSource(ResultSet rs) throws SQLException, ClassNotFoundException {
ResultSetMetaData rsmd = rs. getMetaData();
columnNames. clear();
columnTypes. clear();
data. clear();
int columnCount = rsmd. getColumnCount();
for (int i = 0; i < columnCount; i++) {
columnNames. add(rsmd. getColumnName(i + 1));
Class type = Class. forName(rsmd. getColumnClassName(i + 1));
columnTypes. add(type);
}
fireTableStructureChanged();
while (rs. next()) {
ArrayList rowData = new ArrayList();
for (int i = 0; i < columnCount; i++) {
if (columnTypes. get(i) == String. class)
rowData. add(rs. getString(i + 1));
else
rowData. add(rs. getObject(i + 1));
}
synchronized (data) {
data. add(rowData);
this. fireTableRowsInserted(data. size() - 1, data. size() - 1);
}
}
}
//***TEST***
public static void main(String[] args) {
try {
Class. forName("sun. jdbc. odbc. JdbcOdbcDriver");
Connection con = DriverManager. getConnection("jdbc: odbc: Product","","");
Statement s = con. createStatement();
s. executeQuery("SELECT * FROM Product");
ResultSet rs = s. getResultSet();
DatabaseTableModel model = new DatabaseTableModel();
model. setDataSource(rs);
JTable table = new JTable(model);
JPanel panel = new JPanel(new BorderLayout());
panel. add(new JScrollPane(table), BorderLayout. CENTER);
JFrame frame = new JFrame("Database Table Model");
frame. setLocationRelativeTo(null);
frame. setSize(500, 300);
frame. setContentPane(panel);
frame. setDefaultCloseOperation(JFrame. EXIT_ ON_ CLOSE);
frame. setVisible(true);
} catch (SQLException e) {
e. printStackTrace();
} catch (ClassNotFoundException e) {
e. printStackTrace();
}
}
}
Main method dla zapuska EFT POS:
import java. awt. event.*;
import javax. swing.*;
import java. awt.*;
import java. sql.*;
public class MainMenu extends JFrame {
public static void main(String[] args)
{
Eftpos f = new Eftpos();
f. setTitle("EFT/ POS SYSTEM");
f. show();
}
}
import javax. swing.*;
import java. awt.*;
import java. sql.*;
public class Eftpos extends JFrame
{
private KeyPad pad;
private JTextArea display;
static String url = "jdbc: odbc: Product";
public Eftpos()
{
final int FRAME_ WIDTH = 500;
final int FRAME_ HEIGHT = 200;
setSize(FRAME_ WIDTH, FRAME_ HEIGHT);
addWindowListener(new WindowCloser());
// construct components
pad = new KeyPad();
display = new JTextArea(8, 20);
// add components to content pane
Container contentPane = getContentPane();
contentPane. setLayout(new FlowLayout());
contentPane. add(display);
contentPane. add(pad);
}
private class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent event)
{ System. exit(0);}
}
}
Kod dla key EFT POS keypad:
import java. awt. BorderLayout;
import java. awt. GridLayout;
import java. awt. event. ActionEvent;
import java. awt. event. ActionListener;
import javax. swing. JButton;
import javax. swing. JPanel;
import javax. swing. JOptionPane;
import javax. swing. JTextField;
/**
A GUI component that lets the user enter a number, using
a button pad labeled with digits
*/
public class KeyPad extends JPanel
{
private JPanel buttonPanel;
private JButton clearButton;
private JTextField display;
public KeyPad()
{
setLayout(new BorderLayout());
// add display field
display = new JTextField();
add(display, "North");
// make button panel
buttonPanel = new JPanel();
buttonPanel. setLayout(new GridLayout(4, 3));
// add digit buttons
ActionListener listener = new DigitButtonListener();
addButton("7", listener);
addButton("8", listener);
addButton("9", listener);
addButton("4", listener);
addButton("5", listener);
addButton("6", listener);
addButton("1", listener);
addButton("2", listener);
addButton("3", listener);
// add clear entry button
clearButton = new JButton("CE");
buttonPanel. add(clearButton);
clearButton. addActionListener
(new ClearButtonListener());
addButton("0", listener);
addButton("EN", listener);// needs action implementation
add(buttonPanel, "Center");
}
/**
Gets the value that the user entered.
@return the value in the text field of the keypad
*/
public double getValue()
{// modified by JD to issue error dialog
while ( display. getText().equals("") )
{
String UserInput = JOptionPane. showInputDialog
("Please enter the required input value:");
display. setText(UserInput);
}
return Double. parseDouble(display. getText());
}
/**
Clears the dislay.
*/
public void clear()
{ display. setText("");
}
/**
Adds a button to the button panel
@param label the button label
@param listener the button listener
*/
public void addButton(String label,
ActionListener listener)
{ JButton button = new JButton(label);
buttonPanel. add(button);
button. addActionListener(listener);
}
private class DigitButtonListener implements ActionListener
{ public void actionPerformed(ActionEvent event)
{ // Get the button label
// it is a digit or decimal point
JButton source = (JButton)event. getSource();
String label = source. getText();
display. setText(display. getText() + label);
}
}
private class ClearButtonListener implements ActionListener
{ public void actionPerformed(ActionEvent event)
{ clear();
}
}
}
Kod dla bazi dannih:
import java. util.*;
import java. sql.*;
import java. awt.*;
import javax. swing. table.*;
import javax. swing.*;
public class DatabaseTableModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
private ArrayList<String> columnNames = new ArrayList<String>();
private ArrayList<Class> columnTypes = new ArrayList<Class>();
private ArrayList<ArrayList<Object>> data = new ArrayList<ArrayList<Object>>();
public int getRowCount() {
synchronized (data) {
return data. size();
}
}
public int getColumnCount() {
return columnNames. size();
}
public Object getValueAt(int row, int col) {
synchronized (data) {
return data. get(row).get(col);
}
}
public String getColumnName(int col) {
return columnNames. get(col);
}
public Class getColumnClass(int col) {
return columnTypes. get(col);
}
public boolean isCellEditable(int row, int col) {
return true;
}
public void setValueAt(Object obj, int row, int col) {
synchronized (data) {
data. get(row).set(col, obj);
}
}
/**
* Core of the model. Initializes column names, types, data from ResultSet.
*
* @param rs ResultSet from which all information for model is token.
* @throws SQLException
* @throws ClassNotFoundException
*/
public void setDataSource(ResultSet rs) throws SQLException, ClassNotFoundException {
ResultSetMetaData rsmd = rs. getMetaData();
columnNames. clear();
columnTypes. clear();
data. clear();
int columnCount = rsmd. getColumnCount();
for (int i = 0; i < columnCount; i++) {
columnNames. add(rsmd. getColumnName(i + 1));
Class type = Class. forName(rsmd. getColumnClassName(i + 1));
columnTypes. add(type);
}
fireTableStructureChanged();
while (rs. next()) {
ArrayList rowData = new ArrayList();
for (int i = 0; i < columnCount; i++) {
if (columnTypes. get(i) == String. class)
rowData. add(rs. getString(i + 1));
else
rowData. add(rs. getObject(i + 1));
}
synchronized (data) {
data. add(rowData);
this. fireTableRowsInserted(data. size() - 1, data. size() - 1);
}
}
}
//***TEST***
public static void main(String[] args) {
try {
Class. forName("sun. jdbc. odbc. JdbcOdbcDriver");
Connection con = DriverManager. getConnection("jdbc: odbc: Product","","");
Statement s = con. createStatement();
s. executeQuery("SELECT * FROM Product");
ResultSet rs = s. getResultSet();
DatabaseTableModel model = new DatabaseTableModel();
model. setDataSource(rs);
JTable table = new JTable(model);
JPanel panel = new JPanel(new BorderLayout());
panel. add(new JScrollPane(table), BorderLayout. CENTER);
JFrame frame = new JFrame("Database Table Model");
frame. setLocationRelativeTo(null);
frame. setSize(500, 300);
frame. setContentPane(panel);
frame. setDefaultCloseOperation(JFrame. EXIT_ ON_ CLOSE);
frame. setVisible(true);
} catch (SQLException e) {
e. printStackTrace();
} catch (ClassNotFoundException e) {
e. printStackTrace();
}
}
}
Main method dla zapuska EFT POS:
import java. awt. event.*;
import javax. swing.*;
import java. awt.*;
import java. sql.*;
public class MainMenu extends JFrame {
public static void main(String[] args)
{
Eftpos f = new Eftpos();
f. setTitle("EFT/ POS SYSTEM");
f. show();
}
}