在JInternalFrame中连接数据库。 [英] Connecting Database In JInternalFrame.

查看:66
本文介绍了在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).


这篇关于在JInternalFrame中连接数据库。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆