Hallo Liebes Java Forum,
ich beschäftige mich seit heute Mittag wie ich eine Access Datenbank mit Java verknüpfen kann.
Ich habe versucht die Verbindung aufzubauen aber ohne Erfolg.
Mein Java Code:
Die Klasse Connect
Die Klasse GUI
Mein OS ist Windows 7 64 Bit.
Die ODBC Treiber für Access wurden auch schon als 64 Bit Treiber eingetragen.
Meine Java Version ist Java 7 x64.
Ich hoffe mir kann jemand helfen
mfg
hitman47
ich beschäftige mich seit heute Mittag wie ich eine Access Datenbank mit Java verknüpfen kann.
Ich habe versucht die Verbindung aufzubauen aber ohne Erfolg.
Mein Java Code:
Die Klasse Connect
Java:
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, *.accdb)};DBQ=database.accdb");
JOptionPane.showMessageDialog(null, "Verbindung aufgebaut");
return conn;
} catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}
Die Klasse GUI
Java:
import java.sql.*;
public class gui extends javax.swing.JFrame {
Connection conn = null;
/** Creates new form gui */
public gui() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void formWindowOpened(java.awt.event.WindowEvent evt) {
conn = Connect.ConnectDB();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url]
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new gui().setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}
Mein OS ist Windows 7 64 Bit.
Die ODBC Treiber für Access wurden auch schon als 64 Bit Treiber eingetragen.
Meine Java Version ist Java 7 x64.
Ich hoffe mir kann jemand helfen
mfg
hitman47