SQL Prepared Statement&返回类型

时间:2017-03-31 13:01:38

标签: java mysql jdbc statements

我刚刚在我的类文件中创建了一个方法,我是否将数据插入到我的sql数据库中。

1)这些准备好的陈述是否正确? 2)我需要为方法返回一辆汽车(这可以在哪里完成)? .....由于我现在得到的错误是方法必须返回一个类型Car(Car是类文件的名称)

public Car addVehicle(String aLicense, int aJourneys, String aUsername, String aPassword) {
    Car c = new Car();
    try {

        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection(url + dbName, userName, password);
        statement = conn.createStatement();
        String query = " insert into eflow.registration (cLicense, cJourneys, cUsername, cPassword)"
                + " values (?, ?, ?, ?)";

        PreparedStatement preparedStmt = conn.prepareStatement(query);
        preparedStmt.setString(1, aLicense);
        preparedStmt.setInt(2, aJourneys);
        preparedStmt.setString(3, aUsername);
        preparedStmt.setString(4, aPassword);

        preparedStmt.execute();

        conn.close();

    } catch (Exception e) {
        System.err.println("Got an exception!");
        System.err.println(e.getMessage());

    }
    return c;

}

调用该方法会返回一个错误,该方法不适用于参数

//int addingID = Integer.parseInt(enteringID.getText());
                String addingReg = enteringReg.getText();
                int addingJourneys = Integer.parseInt(enteringJourneys.getText());
                String addingUsername = enteringUsername.getText();
                @SuppressWarnings("deprecation")
                String addingPassword = enteringPassword.getText();
                Car newCar = new Car(addingReg, addingJourneys, addingUsername, addingPassword);
                int addStatus = myCar.addVehicle(newCar);
                if (addStatus == 1) {
                    JOptionPane.showMessageDialog(null, "Vehicle Added");
                    enteringID.setText("(eg. 1-999)");
                    enteringReg.setText("(eg. - 162-MH-749)");
                    enteringJourneys.setText("(eg. 7)");
                    enteringUsername.setText("(eg. - username@domain.com)");
                    enteringPassword.setText("");
                }

                else {
                    JOptionPane.showMessageDialog(null, "Error, Please Try Again");
                }
            } catch (Exception f) {
                JOptionPane.showMessageDialog(null, "Error, Please Try Again");
            }
        }
    });

2 个答案:

答案 0 :(得分:1)

这不是你问题的最终答案,但纯粹是为了澄清我的评论。

如果您希望方法返回Car对象,则必须创建Car类的实例并将其返回:

public Car addVehicle(String aLicense, int aJourneys, String aUsername, String aPassword) {
try {        
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection(url + dbName, userName, password);
    statement = conn.createStatement();
    String query = " insert into eflow.registration (cLicense, cJourneys, cUsername, cPassword)"
            + " values (?, ?, ?, ?)";

    PreparedStatement preparedStmt = conn.prepareStatement(query);
    preparedStmt.setString(1, "'" + aLicense + "'");
    preparedStmt.setInt(2, aJourneys);
    preparedStmt.setString(3, "'" + aUsername + "'");
    preparedStmt.setString(4, "'" + aPassword + "'");

    preparedStmt.execute();

    conn.close();

    Car c = new Car();
    //Do anything with the car object that you like.
    //for example: c.setColor("blue");
    return c;

} catch (Exception e) {
    System.err.println("Got an exception!");
    System.err.println(e.getMessage());
    //kayaman is correct here: we still need to return something here in order to be able to compile
    return null;
}

答案 1 :(得分:0)

分开逻辑!

使用此类可以在您需要的地方检索连接:

public class DatabaseConnection
{
    private static final String CONN_URL = "some connection url";
    private static Connection instance = null;

    static
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
    }

    public static synchronized Connection getInstance() throws SQLException
    {
        if (instance == null)
        {
            instance = DriverManager.getConnection(CONN_URL);
        }
        return instance;
    }
}

在你的函数中使用它:

public Car addVehicle(String aLicense, int aJourneys, String aUsername, String aPassword)
{
    String sql = "insert into eflow.registration (cLicense, cJourneys, cUsername, cPassword) values (?, ?, ?, ?)";

    try (Connection conn = DatabaseConnection.getInstance(); PreparedStatement prepStatement = conn.prepareStatement(sql))
    {
        Car successfulAdd = new Car();

        prepStatement.setString(1, aLicense);
        prepStatement.setInt(2, aJourneys);
        prepStatement.setString(3, aUsername);
        prepStatement.setString(4, aPassword);

        if (prepStatement.execute())
        {
            return successfulAdd;
        }
    }
    catch (SQLException e)
    {
        e.printStackTrace();
    }
    return null;
}