如何在jsp中拆分MySQL数据库连接

时间:2018-05-24 14:59:12

标签: java mysql jsp

我用jsp编写了一个登录页面,但所有段都是混合的。 我想将数据库连接拆分为java类。

Mysql数据库连接代码:

String connectionURL = "jdbc:mysql://localhost:3232/jsp";
Connection connection = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "123456");
if (connection.isClosed()) {
    out.println("Connection Faild !");
}

PreparedStatement ps = (PreparedStatement) connection.prepareStatement("SELECT id FROM jsp_users WHERE user_login = '" + User + "' AND user_password = '" + Pass + "'");
ResultSet rs = ps.executeQuery();
int id = 0;
while (rs.next()) {
    id = rs.getInt("id");
}

0 个答案:

没有答案