gpt4 book ai didi

java - 以编程方式启动 Derby

转载 作者:搜寻专家 更新时间:2023-10-31 19:34:19 26 4
gpt4 key购买 nike

请看下面的代码

DataBaseConnector.java

 import java.sql.*;
import javax.swing.*;

public class DataBaseConnector
{
private Connection con;

public DataBaseConnector()
{

}

private boolean createConnection()
{
try
{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
con = DriverManager.getConnection("jdbc:derby://localhost:1527/contact;create=true","yohanrw","knight");
}
catch(Exception e)
{
System.out.println("Error getConnection");
e.printStackTrace();
JOptionPane.showMessageDialog(null,e.getLocalizedMessage());
return false;
}
return true;
}

private void closeConnection()
{
try
{
con.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e.getLocalizedMessage());
}
}


public void insertData(int id, String firstName, String lastName)
{
createConnection();
try
{
PreparedStatement ps = con.prepareStatement("insert into APP.FRIENDS values(?,?,?)");
ps.setInt(1, id);
ps.setString(2, firstName);
ps.setString(3, lastName);

int result = ps.executeUpdate();

if(result>0)
{
JOptionPane.showMessageDialog(null,"Data Inserted");
}
else
{
JOptionPane.showMessageDialog(null,"Something Happened");
}
}
catch(Exception e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(null,e.getLocalizedMessage());
}
finally
{
closeConnection();
}
}

public void viewData()
{
createConnection();

try
{
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from APP.FRIENDS");

StringBuffer sb = new StringBuffer("");

while(rs.next())
{
sb.append(rs.getInt(1)+"\n");
sb.append(rs.getString(2)+"\n");
sb.append(rs.getString(3)+"\n");


}

JOptionPane.showMessageDialog(null,sb);
}
catch(Exception e)
{



}
}


}

数据库界面

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class DatabaseUI extends JFrame
{
private JLabel firstName, id, lastName;
private JTextField idTxt, firstNameTxt, lastNameTxt;
private JButton ok, view;

public DatabaseUI()
{
firstName = new JLabel("First Name: ");
lastName = new JLabel("Last Name: ");
id = new JLabel("ID: ");

firstNameTxt = new JTextField(10);
lastNameTxt = new JTextField(10);
idTxt = new JTextField(10);

ok = new JButton("ADD");
ok.addActionListener(new OKAction());
view = new JButton("View");
view.addActionListener(new ViewAction());

JPanel centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(4,2));
centerPanel.add(id);
centerPanel.add(idTxt);
centerPanel.add(firstName);
centerPanel.add(firstNameTxt);
centerPanel.add(lastName);
centerPanel.add(lastNameTxt);
centerPanel.add(view);
centerPanel.add(ok);

getContentPane().add(centerPanel,"Center");


this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}

private class OKAction implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
DataBaseConnector db = new DataBaseConnector();

int id = Integer.parseInt(idTxt.getText());

db.insertData(id, firstNameTxt.getText().trim(), lastNameTxt.getText().trim());
}
}

private class ViewAction implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
DataBaseConnector db = new DataBaseConnector();

db.viewData();
}
}



public static void main(String[]args)
{
new DatabaseUI();
}
}

在这种情况下,我需要通过右键单击 database node > start server 来手动启动 derby(我使用的是 NetBeans)。这是一个嵌入式数据库,这意味着我要将它从一台机器带到另一台机器,并且愿意通过双击 jar 文件来启动,而不是在每台机器上配置数据库并手动启动它们。但是,如果我没有手动启动数据库,则会出现错误

java.sql.SQLNonTransientConnectionException: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.

即使在 NetBeans 内部,如果我没有手动启动它,也会出现错误。如何在我的程序中启动 Derby 而无需手动启动它?我尝试了一些方法,例如“create=true”参数、NetworkServer.start(),但效果不佳。但是我不确定我是否做对了。

最佳答案

This is a embedded database, which means I am taking this from one machine to another and willing to start just by double clicking on the jar file,

对于 derby,嵌入式数据库意味着数据库在 JVM 中运行并写入文件系统。这意味着您可以随心所欲地移动 jar 文件,但是如果您使用嵌入式数据库,那么运行该程序的每台机器都将拥有自己独立的数据库,并且只有一个 JVM 可以同时使用该数据库时间。这是你想要的吗?

如果是这样,问题出在程序使用的 URL 上。 “jdbc:derby://localhost:1527/contact;create=true” 是告诉 DriverManager 连接到远程数据库的 URL。程序首先加载嵌入式驱动程序并不重要。

Derby 中的嵌入式 URL 看起来像这样。 jdbc:derby:bar;create=true 它将使用 Derby 系统主目录或当前工作目录之外的 bar 目录中的嵌入式数据库。有关嵌入式 URL 的更多信息,请参阅此处 connecting to a file-based derby database .

关于java - 以编程方式启动 Derby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12668231/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com