Click here to Skip to main content
11,707,470 members (78,493 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Swing
When I am using JInternalFrame where and how am I able to connect a MS Access database using a separate class? I already create a class as the following.

import java.sql.*;
import javax.swing.*;
public class Connect {
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;
public static Connection ConnectDB(){
    try{
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection conn = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)}; DBQ=Database1.mdb");
        JOptionPane.showMessageDialog(null, "Connection Established");
        return conn;
 
    }catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
        return null;
    }
}
 
    
        
    }

The JInternalFrame as

public class ItemDetails extends javax.swing.JInternalFrame{
 
   
 
}


I think in this JInternalFrame's inside I should put my database connecting statements. But Where and how?


Thank You

Chiransj
Posted 23-Dec-12 0:30am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

NO.

That's the constructor. That one is by definition not supposed to execute code.

public class ItemDetails extends JInternalFrame{
 
  private Connection oConnection = null;
 
  public ItemDetails(){
    
  }
 
  private Connection getDBConnection(){
    if(null == oConnection){
      Connection oConnection = Connect.ConnectDB();
    }
    return oConnection;
  }
}

you can at any time access that function and try to get the connection.
If it is not valid, the connection is created new. Interesting, because server connection get sometimes "lost" (e.g. due to exceptions).
  Permalink  
Comments
TorstenH. at 23-Dec-12 7:06am
   
oh - keep the other class for creating the connection. That has nothing to do with the GUI - so it should stay separated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 156
1 Sergey Alexandrovich Kryukov 145
2 Mika Wendelius 140
3 chainerlt 110
4 ppolymorphe 104
0 OriginalGriff 9,178
1 Sergey Alexandrovich Kryukov 8,562
2 CPallini 5,189
3 Maciej Los 4,806
4 Mika Wendelius 3,706


Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 23 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100