繁体   English   中英

如何在NetBeans中连接Java derby数据库

[英]how to connect java derby database in netbeans

我已经在连接中创建了表格,表格名称为“ 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
    }

}

如果您尝试连接到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()方法,一切都应该可以解决。 我希望这会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM