jeg har det her, det virker fint med det, det skal, men jeg vil gerne noget mere.. og jeg kan ikke rigtig få den til det..... så hvis jeg skal lave så jeg kan oprette tabel og database.. hvad skal jeg så skrive ud over det her:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.util.*;
public class Accounts extends JFrame {
private JButton getAccountButton,
insertAccountButton,
deleteAccountButton,
updateAccountButton;
private JList accountNumberList;
private Connection connection;
private JTextField accountIDText,
usernameText,
passwordText,
tsText,
activeTSText;
private JTextArea errorText;
public Accounts() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception e) {
System.err.println("Unable to find and load driver");
System.exit(1);
}
}
private void loadAccounts() {
Vector v = new Vector();
try {
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT kol_id FROM tabellen");
while(rs.next()) {
v.addElement(rs.getString("kol_id"));
}
rs.close();
} catch(SQLException e) {
displaySQLErrors(e);
}
accountNumberList.setListData(v);
}
private void buildGUI() {
Container c = getContentPane();
c.setLayout(new FlowLayout());
accountNumberList = new JList();
loadAccounts();
accountNumberList.setVisibleRowCount(2);
JScrollPane accountNumberListScrollPane = new JScrollPane(accountNumberList);
//Do Get Account Button
getAccountButton = new JButton("Hent Data");
getAccountButton.addActionListener (
new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM tabellen WHERE kol_id = " + accountNumberList.getSelectedValue());
if (rs.next()) {
accountIDText.setText(rs.getString("kol_id"));
usernameText.setText(rs.getString("username"));
passwordText.setText(rs.getString("password"));
tsText.setText(rs.getString("ts"));
activeTSText.setText(rs.getString("act_ts"));
}
} catch(SQLException selectException) {
displaySQLErrors(selectException);
}
}
}
);
//Do Insert Account Button
insertAccountButton = new JButton("Indsæt data");
insertAccountButton.addActionListener (
new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Statement statement = connection.createStatement();
int i = statement.executeUpdate("INSERT INTO tabellen VALUES(" +
accountIDText.getText() + ", " +
"'" + usernameText.getText() + "', " +
"'" + passwordText.getText() + "', " +
"0" + ", " +
"now())");
errorText.append("Handlinger " + i + " Data sat ind\n");
accountNumberList.removeAll();
loadAccounts();
} catch(SQLException insertException) {
displaySQLErrors(insertException);
}
}
}
);
//Do Delete Account Button
deleteAccountButton = new JButton("Slet data");
deleteAccountButton.addActionListener (
new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Statement statement = connection.createStatement();
int i = statement.executeUpdate("DELETE FROM tabellen WHERE kol_id = " +
accountNumberList.getSelectedValue());
errorText.append("handlinger " + i + " Data slettet\n");
accountNumberList.removeAll();
loadAccounts();
} catch(SQLException insertException) {
displaySQLErrors(insertException);
}
}
}
);
//Do Update Account Button
updateAccountButton = new JButton("Update data");
updateAccountButton.addActionListener (
new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Statement statement = connection.createStatement();
int i = statement.executeUpdate("UPDATE tabellen " +
"SET username='" + usernameText.getText() + "', " +
"password='" + passwordText.getText() + "', " +
"act_ts = now() " +
"WHERE kol_id = " + accountNumberList.getSelectedValue());
errorText.append("Handling " + i + " Data updateret\n");
accountNumberList.removeAll();
loadAccounts();
} catch(SQLException insertException) {
displaySQLErrors(insertException);
}
}
}
);
JPanel first = new JPanel(new GridLayout(5,1));
first.add(accountNumberListScrollPane);
first.add(getAccountButton);
first.add(insertAccountButton);
first.add(deleteAccountButton);
first.add(updateAccountButton);
accountIDText = new JTextField(15);
usernameText = new JTextField(15);
passwordText = new JTextField(15);
tsText = new JTextField(15);
activeTSText = new JTextField(15);
errorText = new JTextArea(5, 15);
errorText.setEditable(false);
JPanel second = new JPanel();
second.setLayout(new GridLayout(6,1));
second.add(accountIDText);
second.add(usernameText);
second.add(passwordText);
second.add(tsText);
second.add(activeTSText);
JPanel third = new JPanel();
third.add(new JScrollPane(errorText));
c.add(first);
c.add(second);
c.add(third);
setSize(200,500);
show();
}
public void connectToDB() {
try {
connection = DriverManager.getConnection(
"jdbc:
mysql://localhost/database?user=root&password=nfspower");
} catch(SQLException connectException) {
System.out.println(connectException.getMessage());
System.out.println(connectException.getSQLState());
System.out.println(connectException.getErrorCode());
}
}
private void displaySQLErrors(SQLException e) {
errorText.append("SQLException: " + e.getMessage() + "\n");
errorText.append("SQLState: " + e.getSQLState() + "\n");
errorText.append("VendorError: " + e.getErrorCode() + "\n");
}
private void init() {
connectToDB();
}
public static void main(String[] args) {
Accounts accounts = new Accounts();
accounts.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
accounts.init();
accounts.buildGUI();
}
}