请看下面的代码
数据库连接器.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:连接到端口 1527 上的服务器 localhost 时出错,并显示消息连接被拒绝:连接。
即使在 NetBeans 内部,如果我没有手动启动它,就会出现错误。如何在我的程序中启动 Derby,而无需手动启动它?我尝试了一些方法,比如“ create=true
”参数、NetworkServer.start(),但都不好。但是我不确定我是否正确地做到了。