【问题标题】:how to connect java derby database in netbeans如何在netbeans中连接java derby数据库
【发布时间】:2013-11-19 20:24:56
【问题描述】:

我已经在连接中创建了表,表名是“contact db” 我有添加细节的窗口。这是代码,我想将输入的详细信息添加到我的数据库表中 我不知道如何在 netbeans 中连接到我的表,我能够使用 netbeans 站点中的示例创建表,但连接示例在网络上太模糊了

public void addVendor(String nam,String adres,String num)
{
    l1= new JLabel ("Name");
    l2= new JLabel ("Address");
    l3= new JLabel ("contactNumber");
    Name= new JTextField ("");
    Address= new JTextField ("");
    ContactNumber= new JTextField ("");
    b1 = new JButton("Add");

    border = new BorderLayout();
    this.setLayout(border);
    JPanel Panel = new JPanel(new GridLayout(0,1));
    this.add(Panel);
    Panel.add(l1);
    Panel.add(Name);
    Panel.add(l2);
    Panel.add(Address);
    Panel.add(l3);
    Panel.add(ContactNumber);
    Panel.add(b1);
     b1.addActionListener(this);


     this.setVisible(true);
    this.setSize(425,325);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed( ActionEvent a)
{
    JButton button;
    button = (JButton) a.getSource();
    if (button==b1)
    {
      //here i want to add data to table
    }

}

【问题讨论】:

标签: java sql swing netbeans derby


【解决方案1】:

如果您尝试连接到 Netbeans 中的 Derby 数据库,您需要知道数据库名称(您似乎知道)以及相应的用户名和密码,您应该需要这些信息指定创建数据库的时间。

您还需要知道 URL,这很关键。它应该类似于“jdbc:derby://localhost:1527/YOUR_DATABASE_NAME

另外,请注意,您的数据库名称似乎包含空格,永远不要这样做。只需将其称为 CONTACTSDB 之类的名称即可。

这里有一些希望对你有用的东西。

final String databaseURL = "jdbc:derby://localhost:1527/YourDatabaseName";
final String databaseName = "NameOfYourDatabase";
final String username = "YourUserName";
final String password = "YourPassword";

// Below is the method invoked to establish a connection to the database
public void accessDatabase() throws ClassNotFoundException {

    try {
        Class.forName(rolodexDriver).newInstance();
        connection = DriverManager.getConnection(databaseURL, username, password);
        statement = connection.createStatement();
    } 
    catch (InstantiationException | IllegalAccessException | SQLException ex) {
        Logger.getLogger(rolodexBean.class.getName()).log(Level.SEVERE, null, ex);
    }

} // end of accessDatabase method

然后只需使用 accessDatabase() 方法,我相信一切都会成功。我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    相关资源
    最近更新 更多