Created
March 12, 2013 00:55
-
-
Save knowbody/5139388 to your computer and use it in GitHub Desktop.
Connecting Java with MS Access Database: 1. Click START >>> Control Panel >>> Administrative Tools >>> ODBC Data Sources (32-bit) IMPORTANT: if your Java version is 32-bit you use 32-bit ODBC Data Sources if 64-bit than 64-bit ODBC Data Sources \\\\\ 2. Go to 'System DSN' tab and click 'Add' button and then select the driver Microsoft Access Dri…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.BorderLayout; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.ResultSet; | |
import java.sql.ResultSetMetaData; | |
import java.sql.Statement; | |
import java.util.Vector; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.JScrollPane; | |
import javax.swing.JTable; | |
import javax.swing.table.DefaultTableModel; | |
/* Sample application to show your entire database in JTable */ | |
public class DatabaseInJTable extends JFrame { | |
/** | |
* | |
*/ | |
private static final long serialVersionUID = 1L; | |
public static void main(String[] args) { | |
DatabaseInJTable tab = new DatabaseInJTable(); | |
} | |
public DatabaseInJTable() { | |
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); | |
setTitle("Information Table"); | |
Vector<String> columnNames = new Vector<String>(); | |
Vector<Vector<Object>> data = new Vector<Vector<Object>>(); | |
try (Connection connect = DriverManager | |
.getConnection("jdbc:odbc:yourDatabaseName"); | |
Statement st = connect.createStatement();) { | |
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); | |
String sql = "SELECT * FROM users"; | |
ResultSet rs = st.executeQuery(sql); | |
ResultSetMetaData metaData = rs.getMetaData(); | |
int columns = metaData.getColumnCount(); | |
for (int i = 1; i <= columns; i++) { | |
columnNames.addElement(metaData.getColumnName(i)); | |
} | |
while (rs.next()) { | |
Vector<Object> row = new Vector<Object>(columns); | |
for (int i = 1; i <= columns; i++) { | |
row.addElement(rs.getObject(i)); | |
} | |
data.addElement(row); | |
} | |
} catch (Exception e) { | |
System.out.println(e); | |
} | |
// not editable JTable | |
DefaultTableModel model = new DefaultTableModel(data, columnNames) { | |
/** | |
* | |
*/ | |
private static final long serialVersionUID = 1L; | |
@Override | |
public boolean isCellEditable(int row, int column) { | |
return false; | |
} | |
}; | |
JTable table = new JTable(model); | |
table.setRowSelectionAllowed(true); | |
JScrollPane scrollPane = new JScrollPane(table); | |
getContentPane().add(scrollPane); | |
JPanel buttonPanel = new JPanel(); | |
getContentPane().add(buttonPanel, BorderLayout.SOUTH); | |
pack(); | |
setVisible(true); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.Statement; | |
public class DeleteData { | |
public static void main(String[] args) { | |
try (Connection con = DriverManager | |
.getConnection("jdbc:odbc:yourDatabaseName"); | |
Statement st = con.createStatement();) { | |
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); | |
int i = st.executeUpdate("DELETE FROM users WHERE key=2"); | |
System.out.println("Row is deleted"); | |
} catch (Exception e) { | |
System.out.println(e); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.Statement; | |
class InsertData { | |
public static void main(String[] args) { | |
try (Connection con = DriverManager | |
.getConnection("jdbc:odbc:yourDatabaseName"); | |
Statement st = con.createStatement();) { | |
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); | |
String name = "Smith"; | |
String age = "25"; | |
int i = st.executeUpdate("INSERT INTO users(name,age) VALUES('" | |
+ name + "','" + age + "')"); | |
System.out.println("Row is added"); | |
} catch (Exception e) { | |
System.out.println(e); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.ResultSet; | |
import java.sql.Statement; | |
public class SearchData { | |
public static void main(String[] args) { | |
try (Connection con = DriverManager | |
.getConnection("jdbc:odbc:yourDatabaseName"); | |
Statement st = con.createStatement(); | |
ResultSet rs = st.executeQuery("SELECT * FROM users WHERE key=9");) { | |
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); | |
while (rs.next()) { | |
System.out.println(rs.getString("name") + " " | |
+ rs.getString("age")); | |
} | |
} catch (Exception e) { | |
System.out.println(e); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.Statement; | |
public class UpdateData { | |
public static void main(String[] args) { | |
try (Connection con = DriverManager | |
.getConnection("jdbc:odbc:yourDatabaseName"); | |
Statement st = con.createStatement();) { | |
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); | |
String name = "Zatorski"; | |
String age = "23"; | |
int i = st.executeUpdate("UPDATE users SET name='" + name | |
+ "', age='" + age + "' WHERE key=15"); | |
System.out.println("Row is updated"); | |
} catch (Exception e) { | |
System.out.println(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the instruction above is not complete.