实现 JDBC 实现 JFrame

发布于 2025-01-18 13:28:26 字数 1833 浏览 2 评论 0原文

我需要创建一个简单的应用程序,该应用程序从用户中获取输入并将数据插入SQL Server中的表中。我目前正在尝试使用jframe,但我不完全知道如何实现操作事件并将数据插入数据库。

代码:

// Class 1
// Helper class extending JFrame class
class NewClass extends JFrame {
  
    // JFrame
    static JFrame f;
  
    // JButton
    static JButton b, b1, b2;
  
    // Label to display text
    static JLabel l;
  
    // Main class
    public static void main(String[] args)
    {
        // Creating a new frame to store text field and
        // button
        f = new JFrame("Vet Clinic");
  
        // Creating a label to display text
        l = new JLabel("Viewable Content");
  
        // Creating a new buttons
        b = new JButton("Appointment");
        b1 = new JButton("View Pet History");
        b2 = new JButton("View Appointments");
  
        // Creating a panel to add buttons
        JPanel p = new JPanel();
  
        // Adding buttons and textfield to panel
        // using add() method
        p.add(b);
        p.add(b1);
        p.add(b2);
        p.add(l);
  
        // setbackground of panel
        p.setBackground(Color.red);
  
        // Adding panel to frame
        f.add(p);
  
        // Setting the size of frame
        f.setSize(300, 300);
  
        f.show();
    }
}

我的JDBC连接代码是:

public class JAVACONNECT {
public static void main(String[] args) throws SQLException {
       String url = "jdbc:sqlserver://*****:1433;databaseName=VetClinic";
       String user = "sa";
       String password = "******";
       try{
           Connection connection = DriverManager.getConnection(url,user,password);
           System.out.println("Connected to Microsoft SQL Server");
        } catch (Exception e){
            System.out.println("There's an error");
            e.printStackTrace();   
        }

I am needing to create a simple application that takes input from the user and inserts the data into my table in SQL Server. I am currently trying to use JFrame but I don't exactly know how to implement Action Events and insert data into the database.

Code:

// Class 1
// Helper class extending JFrame class
class NewClass extends JFrame {
  
    // JFrame
    static JFrame f;
  
    // JButton
    static JButton b, b1, b2;
  
    // Label to display text
    static JLabel l;
  
    // Main class
    public static void main(String[] args)
    {
        // Creating a new frame to store text field and
        // button
        f = new JFrame("Vet Clinic");
  
        // Creating a label to display text
        l = new JLabel("Viewable Content");
  
        // Creating a new buttons
        b = new JButton("Appointment");
        b1 = new JButton("View Pet History");
        b2 = new JButton("View Appointments");
  
        // Creating a panel to add buttons
        JPanel p = new JPanel();
  
        // Adding buttons and textfield to panel
        // using add() method
        p.add(b);
        p.add(b1);
        p.add(b2);
        p.add(l);
  
        // setbackground of panel
        p.setBackground(Color.red);
  
        // Adding panel to frame
        f.add(p);
  
        // Setting the size of frame
        f.setSize(300, 300);
  
        f.show();
    }
}

My JDBC Connection code is:

public class JAVACONNECT {
public static void main(String[] args) throws SQLException {
       String url = "jdbc:sqlserver://*****:1433;databaseName=VetClinic";
       String user = "sa";
       String password = "******";
       try{
           Connection connection = DriverManager.getConnection(url,user,password);
           System.out.println("Connected to Microsoft SQL Server");
        } catch (Exception e){
            System.out.println("There's an error");
            e.printStackTrace();   
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眉黛浅 2025-01-25 13:28:26

也许最好花一些时间学习如何在直接跳入数据库实现之前使用摇摆组件。
在Swing中,您在提及“ ActionListeners”时是正确的,这些操作清单被像这样实现:

JButton button = new JButton();
button.addActionListener(new ActionListener() { // this can be replaced by a lambda
    public void actionPerformed(ActionEvent e) {
        // execution code goes here, including jdbc code
    }
});

当您单击按钮时,构成“动作”,并且执行ActionListener中定义的行为。

Perhaps it would be best to spend some time learning how to use Swing components before jumping straight into database implementation.
In Swing, you are correct in mentioning ActionListeners, which are implemented like so:

JButton button = new JButton();
button.addActionListener(new ActionListener() { // this can be replaced by a lambda
    public void actionPerformed(ActionEvent e) {
        // execution code goes here, including jdbc code
    }
});

When you click the button, that constitutes an "action" and the behaviour defined in the ActionListener is performed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文