【问题标题】:How can I avoid sending hardcoded values to mySQL database如何避免将硬编码值发送到 mySQL 数据库
【发布时间】:2021-09-17 10:11:52
【问题描述】:

在我的项目中,我组装了一个基本密码簿,该项目旨在在单击“保存信息”按钮后将用户信息发送到 mySQL 数据库。当字符串值被硬编码时它工作正常,但是当我尝试使用任何类型的字符串变量或 toString() 方法时,我开始收到错误。有谁知道将联合国硬编码值发送到 mySQL 的正确方法?

import javax.swing.*;
//import java.io.*;
//import java.lang.Thread.State;
import java.awt.event.*;
import java.sql.*;

public class PasswordBook implements ActionListener
{
    private static JLabel websiteLabel;
    private static JTextField websiteText;
    private static JLabel userLabel;
    private static JTextField userText;
    private static JLabel passLabel;
    private static JTextField passText;
    private static JLabel emailLabel;
    private static JTextField emailText;
    private static JButton button;
    private static JButton clearButton;
    private static JLabel success;
    //private static String websiteToString; (values I tried)
    //private static String userToString;
    //private static String emailToString;
    //private static String passToString;

    public static void main (String[]args)
    {
        
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        frame.setSize(400,350);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);

        panel.setLayout(null); // rows columns

        websiteLabel = new JLabel("Website"); //1st
        websiteLabel.setBounds(10, 20, 80, 25);
        panel.add(websiteLabel);

        websiteText = new JTextField(); //1st
        websiteText.setBounds(100, 20, 165, 25);
        panel.add(websiteText);
        //websiteToString = websiteText.toString();

        userLabel = new JLabel("Username"); //2nd
        userLabel.setBounds(10, 60, 80 ,25);
        panel.add(userLabel);

        userText = new JTextField(20); // 2nd
        userText.setBounds(100, 60, 165, 25);
        panel.add(userText);
        //userToString = userText.toString();

        emailLabel = new JLabel("Email"); //3rd
        emailLabel.setBounds(10, 100, 80 ,25);
        panel.add(emailLabel);

        emailText = new JTextField(); //2nd
        emailText.setBounds(100, 100, 165, 25);
        panel.add(emailText);
        //emailToString = emailText.toString();

        passLabel = new JLabel("Password"); //4th
        passLabel.setBounds(10, 140, 80, 25);
        panel.add(passLabel);

        passText = new JTextField(); // 4th
        passText.setBounds(100, 140, 165, 25);
        panel.add(passText);
        //passToString = passText.toString();

        button = new JButton("Save Information");
        button.setBounds(10, 180, 150 ,25);
        button.addActionListener(new PasswordBook()); // action listener to button
        panel.add(button);

        clearButton = new JButton("Clear");
        clearButton.setBounds(180, 180, 150, 25);

        // CLEARS THE TEXT FIELDS WHEN PRESSED
        clearButton.addActionListener(new ActionListener() {

            @Override // Override allows function to perfrom independently of the parent class
            public void actionPerformed(ActionEvent event)
            {
                websiteText.setText("");
                userText.setText("");
                emailText.setText("");
                passText.setText("");
                success.setText("");
            }
        });

        panel.add(clearButton);

        success = new JLabel("");
        success.setBounds(10, 220, 300, 25);
        panel.add(success);
        

        frame.setVisible(true);

    }

    public void actionPerformed(ActionEvent event) {
        try {

            Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/passwordbook", "root", "password");
            //Statement myStatement = myConn.createStatement();
            String sq1 = "INSERT into website_and_user_info"
                        + " (Website, Username, Email, Password)"
                        + " values (?, ?, ?, ?)";
            
            PreparedStatement statement = myConn.prepareStatement(sq1);
            statement.setString(1, websiteText.toString()); // These values work when hardcoded
            statement.setString(2, userText.toString());
            statement.setString(3, emailText.toString());
            statement.setString(4, passText.toString());
            
            
            statement.executeUpdate();

            System.out.println("insert complete");
            
        } catch (Exception e) {
            e.printStackTrace();
        }

        

    }


}



【问题讨论】:

  • 不是websiteText.toString,是.getValue
  • .getValue 给我未定义,我需要以某种方式将变量 websiteText 等转换为字符串,然后将这些值传递给 statement.setString()。我已经尝试过将它们转换并发送过去,但也没有用。
  • 您只需要确保所有字段都已填写。我也会在Connection 上使用 Try-With-Resources
  • @Jamesyt60 我的错,我的意思是 'getText() - 只需检查 JTextField 的 API 文档。阅读文档非常有用。
  • 是的,谢谢!我绝对应该通过文档进行更多搜索。

标签: java mysql string hardcoded


【解决方案1】:

如果我没看错的话,passText 是一个 Textfild(建议使用匈牙利符号,使代码更易于阅读)来获取您需要的文本 .gettext()。就像准备好的preparedStatement.setString(int, passText.getText());这应该够了吧。但是您需要在清除字段之前获取文本。你的代码似乎首先清楚,你在声明中使用了 .setTxt 。如果我错了,我很抱歉,atm。我只有我的手机可以浏览。

【讨论】:

  • 不要使用匈牙利符号,它在 java 生态系统中从来没有被普遍使用,一旦你考虑到 95% 以上的 java 开发是在可以着色的 IDE 中完成的,这绝对没有意义或者以其他方式按照您的意愿自动渲染,而且不是惯用的 java。
  • 谢谢 Nasten,你是对的 getText 是正确的使用方法。
  • @rzwitserloot: weil,我是色盲,在这里你没有来自 IDE 的颜色,所以它会有所帮助。 Jamesyt60:很好,很高兴它对你有用。
  • @Nasten1988 作为一个有轻度残疾的人,你应该提倡另一种方式。您希望开发人员能够控制这些事情,而不是样式指南。例如,如果样式指南规定了 2 个空格,而您在分隔小距离时遇到了麻烦,那么您就完蛋了。如果样式指南仅规定缩进的制表符,您可以根据需要配置编辑器的制表位。如果您是色盲,则将您的 IDE 配置为以不同的字体或下划线呈现对静态方法的调用,或者让您感觉良好且可读。
猜你喜欢
  • 2016-05-11
  • 2019-07-06
  • 2022-10-31
  • 1970-01-01
  • 2018-11-19
  • 1970-01-01
  • 2011-02-10
  • 1970-01-01
  • 2011-01-01
相关资源
最近更新 更多