Results 1 to 4 of 4
- 05-13-2014, 06:13 PM #1
Member
- Join Date
- Mar 2014
- Posts
- 17
- Rep Power
- 0
embed MS Access database with runnable jar
So my aim is to make a runnable jar. I've made a program using swings which uses MS Access database to fetch records. So, I've used an absolute path to refer to the database for connection. Now, I intend to distribute this runnable jar to other people as well. So I guess the best option would be to embed the MS Access database as well in the jar file. But I don't know how to do that. Where should I keep the database in my Project Explorer ? Should I use a relative path etc. Any form of help would be appreciable.
This is my code:-
Java Code:import java.awt.*; import java.awt.event.*; import java.sql.*; import java.util.Vector; import javax.swing.*; import javax.swing.table.DefaultTableModel; public class r_search extends JFrame implements ActionListener { JFrame frame1; JLabel l0, l1, l2; JComboBox c1; JButton b1; Connection con; ResultSet rs, rs1; Statement st, st1; PreparedStatement pst; String ids; static JTable table; String[] columnNames = {"SECTION NAME", "REPORT NAME", "CONTACT", "LINK"}; String from; r_search() { l0 = new JLabel("Fetching Search Results..."); l0.setForeground(Color.blue); l0.setFont(new Font("Serif", Font.BOLD, 20)); l1 = new JLabel("Search"); b1 = new JButton("submit"); l0.setBounds(100, 50, 350, 40); l1.setBounds(75, 110, 75, 20); b1.setBounds(150, 150, 150, 20); b1.addActionListener(this); setTitle("Search Executive Reports :) "); setLayout(null); //setVisible(true); setSize(500, 500); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); add(l0); add(l1);; add(b1); try { Vector v = new Vector(); String url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + "C:\\users\\ppreeti\\executive_db.accdb"; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection(url,"",""); st = con.createStatement(); rs = st.executeQuery("select index_name from Index1"); // Vector v = new Vector(); while (rs.next()) { ids = rs.getString(1); v.add(ids); } c1 = new JComboBox(v); c1.setBounds(150, 110, 150, 20); add(c1); st.close(); rs.close(); } catch (Exception e) { } setVisible(true); } public void actionPerformed(ActionEvent ae) { if (ae.getSource() == b1) { showTableData(); } } public void showTableData() { frame1 = new JFrame("Database Search Result"); frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame1.setLayout(new BorderLayout()); //TableModel tm = new TableModel(); DefaultTableModel model = new DefaultTableModel(); model.setColumnIdentifiers(columnNames); //DefaultTableModel model = new DefaultTableModel(tm.getData1(), tm.getColumnNames()); //table = new JTable(model); table = new JTable(); table.setModel(model); table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); table.setFillsViewportHeight(true); JScrollPane scroll = new JScrollPane(table); scroll.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); scroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); from = (String) c1.getSelectedItem(); //String textvalue = textbox.getText(); String uname = ""; String email = ""; String pass = ""; String cou = ""; try { pst = con.prepareStatement("select distinct Section.Section_Name,Report.Report_Name,Report.Link,Contact.Contact_Name " + "FROM (( Section INNER JOIN Report ON Report.Section_ID=Section.Section_ID ) INNER JOIN Contact ON Contact.Contact_ID=Report.Contact_ID ) LEFT JOIN Metrics ON Metrics.Report_ID=Report.Report_ID " + " WHERE Section.Section_Name LIKE '%"+from+"%' OR Report.Report_Name LIKE '%"+from+"%' OR Metrics.Metric_Name LIKE '%"+from+"%' OR Contact.Contact_Name LIKE '%"+from+"%' "); ResultSet rs = pst.executeQuery(); int i = 0; while (rs.next()) { uname = rs.getString("Section_Name"); email = rs.getString("Report_Name"); pass = rs.getString("Contact_Name"); cou = rs.getString("Link"); model.addRow(new Object[]{uname, email, pass, cou}); i++; } if (i < 1) { JOptionPane.showMessageDialog(null, "No Record Found", "Error", JOptionPane.ERROR_MESSAGE); } if (i == 1) { System.out.println(i + " Record Found"); } else { System.out.println(i + " Records Found"); } } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); } frame1.add(scroll); frame1.setVisible(true); frame1.setSize(1000, 400); } public static void main(String args[]) { new r_search(); } }
Last edited by ppreeti; 05-13-2014 at 06:19 PM.
- 05-13-2014, 06:26 PM #2
Re: embed MS Access database with runnable jar
I don't think putting a DB in the jar file will work. The DB file will need to be a separate file.
BTW All catch blocks should have a call to the printStackTrace() method so messages are printed when there is an exception.If you don't understand my response, don't ignore it, ask a question.
- 05-13-2014, 06:29 PM #3
Member
- Join Date
- Mar 2014
- Posts
- 17
- Rep Power
- 0
Re: embed MS Access database with runnable jar
Okay..I understand that. but tell me one thing ! My requirement is to send this to clients as well. So is it the best way to send the database to them as well ? Are there any other alternatives given my requirement ? And suppose I do so, then how to set the path to the database in relative mode ? (considering that when I send the database to them too supplying an absolute path to database wouldn't work) Any suggestions in this regard ?
- 05-13-2014, 06:47 PM #4
Similar Threads
-
Runnable jar doenst show data from database
By bodylojohn in forum New To JavaReplies: 5Last Post: 03-13-2014, 11:56 AM -
how to embed combobox,checbox in JTable read write to database
By scorpion.poison in forum New To JavaReplies: 4Last Post: 08-30-2012, 11:52 AM -
How to convert access database to mysql database?
By vrk in forum Advanced JavaReplies: 2Last Post: 02-11-2009, 05:43 AM -
Database access
By Doctor Cactus in forum New To JavaReplies: 1Last Post: 12-12-2008, 02:30 PM -
Help with access a database using Microsoft Access
By cachi in forum JDBCReplies: 1Last Post: 08-07-2007, 08:51 AM
Bookmarks