如何在netbeans中连接JavaDerby数据库
我已经在连接中创建了表,表名为“contact db” 我有一个窗口,可以添加细节。这是代码,我想将输入的详细信息添加到我的数据库表中 我不知道如何在netbeans中连接到我的表,我可以使用netbeans站点的一个示例创建表,但是在网络上连接示例太模糊了如何在netbeans中连接JavaDerby数据库,java,sql,swing,netbeans,derby,Java,Sql,Swing,Netbeans,Derby,我已经在连接中创建了表,表名为“contact db” 我有一个窗口,可以添加细节。这是代码,我想将输入的详细信息添加到我的数据库表中 我不知道如何在netbeans中连接到我的表,我可以使用netbeans站点的一个示例创建表,但是在网络上连接示例太模糊了 public void addVendor(String nam,String adres,String num) { l1= new JLabel ("Name"); l2= new JLabel ("Address");
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/您的\u数据库\u名称“ 另外,请注意,您的数据库名称似乎包含一个空格,永远不要这样做。叫它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()方法,我相信一切都会解决。我希望这能有所帮助 这可能会对你有所帮助。由于某些原因,这会在网络上产生很多错误。你会遇到哪些错误?在实现这一部分时,您到底面临什么问题?