在JInternalFrame中连接数据库。
当我使用JInternalFrame时,我在哪里以及如何使用单独的类连接MS Access数据库?我已经创建了一个类如下。
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;
}
}
}
JInternalFrame为
The JInternalFrame as
public class ItemDetails extends javax.swing.JInternalFrame{
}
我想在这个JInternalFrame里面我应该把我的数据库连接语句。但是在哪里以及如何?
谢谢
Chiransj
I think in this JInternalFrame''s inside I should put my database connecting statements. But Where and how?
Thank You
Chiransj
否 。
那''是构造函数。根据定义,那个不应该执行代码。
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).