如何将结构更改为JDBC模板?

时间:2015-03-26 08:13:29

标签: java spring jdbc

如何使用JDBC模板并在单独的类中找到它。我想在一个地方与DB建立连接并将其分享给其他类。

感谢您的帮助和关注!

这是view bean的一个方法,它从dao类调用方法。

  public String addNewUser() {
    ELContext elContext = FacesContext.getCurrentInstance().getELContext();
    ViewBean viewBean = (ViewBean) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, "viewBean");
    UserDao ud = new UserDao();
    ud.addUserToDb(viewBean.getUser());
    return sessionBean.redirectToLogin();}

这是我的道教课程

public void addUserToDb(User user) {
    int i = 0;
    PreparedStatement ps = null;
    Connection con = null;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/mrktplc";
        con = DriverManager.getConnection(url, "root", "qwer1234");
        if (con != null) {
            String sql = "INSERT INTO user_account(user_name, family_name, login, password, email) VALUES(?,?,?,?,?)";
            ps = con.prepareStatement(sql);

            ps.setString(1, user.getUserName());
            ps.setString(2, user.getFamilyName());
            ps.setString(3, user.getLoginName());
            ps.setString(4, user.getPassword());
            ps.setString(5, user.getEmail());
            i = ps.executeUpdate();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "User added successfully", ""));
            System.out.println("User added successfully");
        }
    } catch (Exception e) {
        System.out.println(e);
    } finally {
        try {
            con.close();
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 public void updateUserInDb (User user) {
    int i = 0;
    PreparedStatement ps = null;
    Connection con = null;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/mrktplc";
        con = DriverManager.getConnection(url, "root", "qwer1234");
        if (con != null) {
            System.out.println(user.getUserName());

            String sql = "UPDATE user_account SET user_name=?, family_name=?, email=?, address=?, photo=? WHERE user_account_id=" + user.getUserId() + "";
            ps = con.prepareStatement(sql);
            ps.setString(1, user.getUserName());
            ps.setString(2, user.getFamilyName());
            ps.setString(3, user.getEmail());

            ps.setString(4, user.getAddress());
            String fileName = null;
            if (user.getUploadedFile() != null) {
                fileName = new FileUploader().fileUpload(user.getUploadedFile());
            }
            ps.setString(5, fileName);

            i = ps.executeUpdate();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", "User updated successfully!"));
            System.out.println("Personal data is updated");
        }

    } catch (Exception e) {
        System.out.println(e);
    } finally {
        try {
            con.close();
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

不确定它是否是您想要的,但如果是,那么:您可以使用两种方法创建一个新类DBUtils,用于获取连接并按如下方式关闭它们:

public class DBUtils {

        public static Connection getConnection() {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                String url = "jdbc:mysql://localhost:3306/mrktplc";
                return DriverManager.getConnection(url, "root", "qwer1234");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        public static void closeConnPstmt(Connection conn, PreparedStatement pstmt) {
            try {
                if (conn != null) {
                    conn.close();
                }
                if (pstmt != null) {
                    pstmt.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }       
}

现在您可以使用getConnection()来获取连接,在finally块中,您可以调用closeConnPstmt()来关闭Connection和PreparedStatement。