实现 JDBC 实现 JFrame
我需要创建一个简单的应用程序,该应用程序从用户中获取输入并将数据插入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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许最好花一些时间学习如何在直接跳入数据库实现之前使用摇摆组件。
在Swing中,您在提及“ ActionListeners”时是正确的,这些操作清单被像这样实现:
当您单击按钮时,构成“动作”,并且执行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:
When you click the button, that constitutes an "action" and the behaviour defined in the ActionListener is performed.