【问题标题】:how to connect my awt java form with mysql database?如何将我的 awt java 表单与 mysql 数据库连接?
【发布时间】:2019-06-15 03:28:05
【问题描述】:

我正在尝试使用 java awt 创建一个表单应用程序。然后我想在我的数据库中添加从表单中获取的数据。 我在按钮 btnadd 中使用了 ActionListener,它应该连接到数据库并存储在其中

这是用于插入数据的awt表单

import java.awt.*;
import java.awt.event.*;

public class InsertFrom extends Frame{
Panel panelInsert,panelbtn;
Label lfName,lmName,llName,lage,lssn,lcity,lstate,lcountry;
TextField tfName,tmName,tlName,tage,tssn,tcity,tstate,tcountry;
Button btnadd;
String firstName,middleName,lastName,city,state,country;
int age,ssn;
InsertFrom(){
   lfName = new Label("First Name");
   lmName = new Label("Middle Name");
   llName = new Label("Last Name");
   lage = new Label("Age");
   lssn = new Label("SSN");
   lcity = new Label("City");
   lstate = new Label("State");
   lcountry = new Label("Country");
   tfName = new TextField(10);
   tmName = new TextField(10);
   tlName = new TextField(10);
   tage = new TextField(10);
   tssn = new TextField(10);
   tcity = new TextField(10);
   tstate = new TextField(10);
   tcountry = new TextField(10);
   panelInsert = new Panel(new GridLayout(8,2));
   panelInsert.add(lfName);
   panelInsert.add(tfName);
   panelInsert.add(lmName);
   panelInsert.add(tmName);
   panelInsert.add(llName);
   panelInsert.add(tlName);
   panelInsert.add(lage);
   panelInsert.add(tage);
   panelInsert.add(lssn);
   panelInsert.add(tssn);
   panelInsert.add(lcity);
   panelInsert.add(tcity);
   panelInsert.add(lstate);
   panelInsert.add(tstate);
   panelInsert.add(lcountry);
   panelInsert.add(tcountry);

   btnadd = new Button("Add to Database");
   panelbtn = new Panel(new FlowLayout());
   panelbtn.add(btnadd);
   btnadd.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent e){
           firstName=tfName.getText();
           middleName=tmName.getText();
           lastName=tlName.getText();
           age=Integer.parseInt(tage.getText());
           ssn=Integer.parseInt(tssn.getText());
           city=tcity.getText();
           state=tstate.getText();
           country=tcountry.getText();
           InsertingData();
       }
   });

   addWindowListener(new WindowAdapter(){
       public void windowClosing(WindowEvent we){
        System.exit(0);
       }
   });
   add(panelInsert);
   add(panelbtn);
   setTitle("Insert Form");
   setLayout(new GridLayout(2,1));
   setVisible(true);
   setSize(500,500);
   setLocationRelativeTo(null);
}
public static void main(String[] args){
    new InsertFrom();
}

}

这是为了连接数据库并在数据库中插入数据

import java.io.*;
import java.sql.*;
import java.util.StringTokenizer;

public class InsertingData { 
public static void main(String args[]) throws Exception{
    String sql="INSERT INTO customers VALUES ('"+firstName+"','"
            +middleName+"','"+lastName+"',"+age+","+ssn+",'"
            +city+"',"+state+",'"+country+"')";
    insertCustomer(sql);
}
private static void insertCustomer(String sql) throws Exception{
    Class.forName("com.mysql.jdbc.Driver");
    String url="jdbc:mysql://localhost:3306/mydb";
    Connection con =DriverManager.getConnection(url,"root","");
    if(con!=null){
        Statement stmt =con.createStatement();
        int result= stmt.executeUpdate(sql);
        if(result!=-1){
            System.out.println("Inserted"+result+"Record(s) successfully");
        }else{
            System.out.println("Unable to insert record. Please check your 
SQL syntax");
        }

    stmt.close();
    con.close();
    }else{
        System.out.println("Unable to get the connection");
    }
 }
}

【问题讨论】:

  • 请记住,Java 应用程序可以被反编译,这使得获取 MySQL 用户名/密码非常容易。此外,您的应用程序容易受到 SQL 注入,您需要查看准备好的语句。

标签: java mysql awt actionlistener


【解决方案1】:

InsertingData 中更改您的 main 方法名称。如果您不想从那时开始应用,请尽量避免使用 main 关键字。

你可以这样做:

public static void insertData(String firstName, String middleName, 
                              String lastName, int age, int ssn, String city
                              String state, String country) throws Exception{

    String sql="INSERT INTO customers VALUES ('"+firstName+"','"
            +middleName+"','"+lastName+"',"+age+","+ssn+",'"
            +city+"',"+state+",'"+country+"')";

    insertCustomer(sql);

}

然后在你的ActionListener 中从InsertFrom 中调用它,如下所示:

InsertingData.insertData(firstName, middleName, lastName, age, ssn, city, state, country);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    相关资源
    最近更新 更多