1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

JAVA Tables

Discussion in 'Programming & Webmastering' started by mars, Jun 17, 2011.

  1. mars New Member

    Joined:
    May 12, 2011
    Messages:
    11 (0.01/day)
    Thanks Received:
    0
    hi! i want to make a table in java that is connected to an MS Access database. I want this table to have such functionalities.

    1) a search bar where the table is automatically filtered once a letter/letters is typed.
    2) the table should be editable / accept changes
    3) the table can have an add/edit/delete functionality.

    most likely i want to do something like this with an additional search button

    but http://www.javaguicodexample.com/javadesktopguimysql1.html

    problem is it is using SQL and i am required to use access. Thank you. :)
    Last edited by a moderator: Feb 19, 2014
  2. Zyon

    Zyon New Member

    Joined:
    Mar 18, 2011
    Messages:
    264 (0.21/day)
    Thanks Received:
    29
    Location:
    Australia
  3. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (5.03/day)
    Thanks Received:
    5,615
    Location:
    Cheeseland (Wisconsin, USA)
    When I clicked on the link in the OP my AV blocked (intrusion prevention) a web attack.
    Could be a compromised site (driveby toolkit download injected into site).
    I unparsed the link so no one clicks on it until it can be verified.
    stinger608 says thanks.
  4. mars New Member

    Joined:
    May 12, 2011
    Messages:
    11 (0.01/day)
    Thanks Received:
    0
    I already see my records from the database, thing is i don't know the codes to give the right functionality of the search text field. i can send the codes if you want me to. BTW, im using Vectors. i just need help. :) thanks very much
  5. mars New Member

    Joined:
    May 12, 2011
    Messages:
    11 (0.01/day)
    Thanks Received:
    0
    can anyone help me now about sorting. when the user clicks the column header the table should be then sorted. any idea?
  6. Zyon

    Zyon New Member

    Joined:
    Mar 18, 2011
    Messages:
    264 (0.21/day)
    Thanks Received:
    29
    Location:
    Australia
    Assuming Java works the same way as C#, make an event handler for the column you want to be sorted and then do the sorting action when the event fires by clicking the column header.
  7. mars New Member

    Joined:
    May 12, 2011
    Messages:
    11 (0.01/day)
    Thanks Received:
    0
    can you give me a step by step procedure, i really don't have an idea. this is my code

    public void getInfo()
    {
    try {
    PreparedStatement pre = null;
    for (int ctr2 = 0; ctr2 < Search.jTable1.getColumnCount(); ctr2++) {
    for (int ctr1 = 0; ctr1 < Search.jTable1.getRowCount(); ctr1++) {
    Search.jTable1.setValueAt("", ctr1, ctr2);
    }
    }

    Connection conn = dbConnection();
    //kung anu ung sinelect sa combobox galing sa search.java
    if (Search.jComboBox1.getSelectedItem().toString().equals("ALL")){
    pre = conn.prepareStatement("select * from Info");
    }
    if (Search.jComboBox1.getSelectedItem().toString().equals("Last Name")){
    pre = conn.prepareStatement("select * from Info where lastname like'%" + se + "%'");}
    else if (Search.jComboBox1.getSelectedItem().toString().equals("First Name")){
    pre = conn.prepareStatement("select * from Info where firstname like'%" + se + "%'");
    }
    else if (Search.jComboBox1.getSelectedItem().toString().equals("Middle Initial")){
    pre = conn.prepareStatement("select * from Info where middleI like '%" + se + "%'");
    }
    else if (Search.jComboBox1.getSelectedItem().toString().equals("Age")){
    pre = conn.prepareStatement("select * from Info where age like'%" + se + "%'");
    }
    else if (Search.jComboBox1.getSelectedItem().toString().equals("Address")){
    pre = conn.prepareStatement("select * from Info where address like '%" + se + "%'");
    }
    else if (Search.jComboBox1.getSelectedItem().toString().equals("Course")){
    pre = conn.prepareStatement("select * from Info where course like'%" + se + "%'");
    }
    else if (Search.jComboBox1.getSelectedItem().toString().equals("Contact No")){
    pre = conn.prepareStatement("select * from Info where contactno like '%" + se + "%'");
    }
    else if (Search.jComboBox1.getSelectedItem().toString().equals("Position")){
    pre = conn.prepareStatement("select * from Info where position like '%" + se + "%'");
    }
    else if (Search.jComboBox1.getSelectedItem().toString().equals("Remarks")){
    pre = conn.prepareStatement("select * from Info where remarks like'%" + se + "%'");
    }



    ResultSet result = pre.executeQuery();
    //display result sa search.java based sa tinype sa textbox
    int rowctr = 0;
    while (result.next()) {
    Search.jTable1.setValueAt(result.getString("lastname"), rowctr, 0);
    Search.jTable1.setValueAt(result.getString("firstname"), rowctr, 1);
    Search.jTable1.setValueAt(result.getString("middleI"), rowctr, 2);
    Search.jTable1.setValueAt(result.getString("age"), rowctr, 3);
    Search.jTable1.setValueAt(result.getString("address"), rowctr, 4);
    Search.jTable1.setValueAt(result.getString("course"), rowctr, 5);
    Search.jTable1.setValueAt(result.getString("contactno"), rowctr, 6);
    Search.jTable1.setValueAt(result.getString("position"), rowctr, 7);
    Search.jTable1.setValueAt(result.getString("remarks"), rowctr, 8);


    rowctr++;
    }
    }
    catch (Exception e) {
    //JOptionPane.showMessageDialog(se+ "NotFound"), se, se, messageType);
    }
    }

    ///////////////////////////////////////////////////////////////////////////


    public Vector getInfo()throws Exception{
    Vector<Vector<String>> InfoVector = new Vector<Vector<String>>();
    String se = jTextField1.getText();
    String database = "jdbc:eek:dbc:Driver={Microsoft Access Driver (*.mdb)}; DBQ=C:/Users/OJT/Documents/db.mdb";
    Connection conn = DriverManager.getConnection(database);
    PreparedStatement pre = conn.prepareStatement("select * from Info ");
    ResultSet rs = pre.executeQuery();

    while(rs.next())
    {
    Vector<String> Info = new Vector<String>();
    Info.add(rs.getString(1));
    Info.add(rs.getString(2));
    Info.add(rs.getString(3));
    Info.add(rs.getString(4));
    Info.add(rs.getString(5));
    Info.add(rs.getString(6));
    Info.add(rs.getString(7));
    Info.add(rs.getString(8));
    Info.add(rs.getString(9));
    InfoVector.add(Info);
    }
    if(conn!=null)
    conn.close();
    return InfoVector;
    }
  8. Zyon

    Zyon New Member

    Joined:
    Mar 18, 2011
    Messages:
    264 (0.21/day)
    Thanks Received:
    29
    Location:
    Australia
    TL;DR :O

    Nah just kidding, I have yet to try out making a database access program in Java so unfortunately I can't really help you with the details as of yet. However this page might help you better, it's about making event listeners on Java ui objects and when you click something that has the listener, the listener's action will be performed.

    http://download.oracle.com/javase/tutorial/uiswing/events/intro.html


    EDIT: actually you're better off using something that has all the functionalities, C#'s DataGridView has a whole bunch of stuff like the 'sort' column headers that you can click and make it sort things, maybe find an equivalent for Java and it should stop your headaches about event listeners.

    EDIT2: Wow, did you really need such a long chain of If's just to do a select? Try working with case select, the readability is better, although one of the better idea would be loading a configuration file that tells program what to do when 'xxx' is selected, but in any case it is up to you.
    Last edited: Jun 21, 2011
  9. mars New Member

    Joined:
    May 12, 2011
    Messages:
    11 (0.01/day)
    Thanks Received:
    0
    Thanks. Great Help. :D
  10. mars New Member

    Joined:
    May 12, 2011
    Messages:
    11 (0.01/day)
    Thanks Received:
    0
    already done with the sorting. and yup jtables have an auto sorter problem is it includes the null values in the sorting. anyway. what i want to ask now is for the delete part of my table my code goes like this:

    private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {
    // TODO add your handling code here:
    try {
    // TODO add your handling code here:
    String SQL = "DELETE FROM Info WHERE ID='"+jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString()+"'";
    jTable1.setValueAt("", jTable1.getSelectedRow(), 0);
    jTable1.setValueAt("", jTable1.getSelectedRow(), 1);
    jTable1.setValueAt("", jTable1.getSelectedRow(), 2);
    jTable1.setValueAt("", jTable1.getSelectedRow(), 3);
    jTable1.setValueAt("", jTable1.getSelectedRow(), 4);
    jTable1.setValueAt("", jTable1.getSelectedRow(), 5);
    jTable1.setValueAt("", jTable1.getSelectedRow(), 6);
    jTable1.setValueAt("", jTable1.getSelectedRow(), 7);
    jTable1.setValueAt("", jTable1.getSelectedRow(), 8);
    jTable1.setValueAt("", jTable1.getSelectedRow(), 9);
    jTable1.setValueAt("", jTable1.getSelectedRow(), 10);
    jTable1.setValueAt("", jTable1.getSelectedRow(), 11);
    jTable1.setValueAt("", jTable1.getSelectedRow(), 12);
    jTable1.setValueAt("", jTable1.getSelectedRow(), 13);


    PreparedStatement stmt = conn.prepareStatement(SQL);
    int executeUpdate = stmt.executeUpdate();


    JOptionPane.showMessageDialog(null, "RECORD successfully deleted!", "Information",
    JOptionPane.INFORMATION_MESSAGE);
    } catch (SQLException ex) {
    Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex);
    }
    }

    when i click the delete button it will delete the record but an error will occur the error is:

    Have no file for C:\Program Files\Java\jdk1.6.0_25\jre\lib\modules\jdk.boot.jar
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at myjava.Search.jButton3MouseClicked(Search.java:364)
    at myjava.Search.access$500(Search.java:29)
    at myjava.Search$7.mouseClicked(Search.java:265)
    at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253)
    at java.awt.Component.processMouseEvent(Component.java:6291)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6053)
    at java.awt.Container.processEvent(Container.java:2041)
    at java.awt.Component.dispatchEventImpl(Component.java:4651)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4481)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4247)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.java:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4481)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
    at java.awt.EventQueue.access$000(EventQueue.java:84)
    at java.awt.EventQueue$1.run(EventQueue.java:602)
    at java.awt.EventQueue$1.run(EventQueue.java:600)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:616)
    at java.awt.EventQueue$2.run(EventQueue.java:614)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


    well i checked my database and the file is not deleted. Help me please. Thank you.
  11. Msap14 New Member

    Joined:
    Nov 11, 2008
    Messages:
    156 (0.07/day)
    Thanks Received:
    10
    Location:
    US
    not sure if this will help or not, but a quick search led me to this:

    http://www.scriptiny.com/2009/03/table-sorter/

    it seems to be versatile, i did not take a good look at it yet but it has a lot of features you may be interested in.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page