简体   繁体   English

如何避免将硬编码值发送到 mySQL 数据库

[英]How can I avoid sending hardcoded values to mySQL database

In my project I've assembled a basic password book, the project is designed to send user information over to a mySQL database after the "Save Information" button has been clicked.在我的项目中,我已经组装了一个基本的密码簿,该项目旨在在单击“保存信息”按钮后将用户信息发送到 mySQL 数据库。 It works fine when the String values are hardcoded, but when I try to use any kind of String variable or the toString() method I start getting errors.当 String 值被硬编码时它工作正常,但是当我尝试使用任何类型的 String 变量或 toString() 方法时,我开始收到错误。 Does anyone know the correct way to send UN-hardcoded values to mySQL?有谁知道将 UN 硬编码值发送到 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();
        }

        

    }


}



If I read it right, passText is a Textfild (advice use Hungarian notation, makes code easier to read) to get the text you need .gettext().如果我没看错,passText 是一个 Textfild(建议使用匈牙利表示法,使代码更易于阅读)来获取您需要的文本 .gettext()。 Like prepared preparedStatement.setString(int, passText.getText());就像准备好的preparedStatement.setString(int, passText.getText()); that should do the trick.这应该够了吧。 But you need to get text before clearing the fields.但是您需要在清除字段之前获取文本。 You Code seems to first clear and you use .setTxt in the statement.您的代码似乎首先清除,然后在语句中使用 .setTxt。 If iam wrong I'm sry, atm.i just have my phone to browse.如果我错了,我很抱歉,atm.i 只是让我的手机浏览。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让 ChromeDriver 路径不被硬编码? - How can I have the ChromeDriver path not be hardcoded? 如何将值(JTextField和JXDatePicker)存储到MySQL数据库中? - How can I store the values(JTextField & JXDatePicker) into MySQL database? 如何管理 Firebase Firestore 中的可选值并避免数据库中的空字段? - How I can manage optional values in Firebase Firestore and avoid empty fields in database? 如何在Axis生成的代码中避免使用硬编码的url? - How to avoid hardcoded url in Axis generated code? 如何使用硬编码库重命名该Android Project - How can I rename this Android Project with hardcoded libraries 如何使用ResourceBundle来避免Java应用程序中的硬编码配置路径? - How do I use ResourceBundle to avoid hardcoded config paths in Java apps? 输入回收者视图时,如何避免活动中的硬编码字符串? - How do I avoid hardcoded strings in activity while inputting for recycler view? 如何添加发送消息的时间(Spring Boot 和 MySQL) - How can I add the time of sending a message (Spring Boot and MySQL) 我无法让我的插件在我的 MySQL 数据库中存储值 - I can not get my plugin to store values in my MySQL database 如何更改 MySQL 会话数据库? - How can I change MySQL session database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM