要求,在编译器中通过JDBC驱动连接数据库并对其进行增删改查
连接工具类
package com.aroad.webdemo.DJBCtest;
import java.io.FileReader;
import java.net.URL;
import java.sql.*;
import java.util.Properties;
public class JDBCUtils {
private static String url;
private static String user;
private static String password;
private static String driver;
static {
try {
Properties properties = new Properties();
ClassLoader classLoader = JDBCUtils.class.getClassLoader();
URL res = classLoader.getResource("jdbc.properties");
String path = res.getPath();
System.out.println(path);
properties.load(new FileReader(path));
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("password");
driver = properties.getProperty("driver");
Class.forName(driver);
}catch (Exception e){
e.printStackTrace();
}
}
public static Connection getCounection() throws SQLException {
return DriverManager.getConnection(url,user,password);
}
public static void close(ResultSet rs, Statement stmt, Connection conn) throws SQLException{ //ResultSet 结果集
if(rs != null){
rs.close();
stmt.close();
}
if (stmt != null){
stmt.close();
}
if(conn != null){
conn.close();
}
}
}
增删改查操作类
package com.aroad.webdemo.DJBCtest;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcTest {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
}
public boolean register(String username,String password) throws SQLException{
if (username == null || password == null)
return false;
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
connection = JDBCUtils.getCounection();
String sql = "SELECT * FROM user WHERE username = '"+ username+"'";
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
if (resultSet.next()){
return false;
}
sql = "INSERT INTO user (username,password,authrity) values ('"+username+"','"+password+",'+'5')";
statement = connection.createStatement();
if (statement.executeUpdate(sql) >= 1)
return true;
}catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(null,statement,connection);
JDBCUtils.close(resultSet,statement,connection);
}
return false;
}
public boolean login(String username,String password) throws SQLException{
if (username == null || password == null)
return false;
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
connection = JDBCUtils.getCounection();
String sql = "SELECT * FROM user WHERE username = '"+ username+"' AND password = '"+password+"'";
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
return resultSet.next();
}catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(null,statement,connection);
JDBCUtils.close(resultSet,statement,connection);
}
return false;
}
public boolean changePwd(String username,String oldPassword,String newPassword) throws SQLException{
if (username == null || oldPassword == null || newPassword == null)
return false;
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
connection = JDBCUtils.getCounection();
String sql = "SELECT * FROM user WHERE username = '"+ username+"' AND password = '"+oldPassword+"'";
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
if (resultSet.next()){
sql = "UPDATE user SET password = '"+newPassword+"' WHERE username = '"+username+"'";
resultSet = statement.executeQuery(sql);
return resultSet.next();
}
return false;
}catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(null,statement,connection);
JDBCUtils.close(resultSet,statement,connection);
}
return false;
}
public boolean deleteUser(String username) throws SQLException{
if (username == null)
return false;
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
connection = JDBCUtils.getCounection();
String sql = "DELETE FROM user WHERE usaername = '"+username+"'";
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
}catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(null,statement,connection);
JDBCUtils.close(resultSet,statement,connection);
}
return false;
}
}
这篇博客介绍了如何在Java编程环境中,通过JDBC连接工具类实现与MySQL数据库的连接,进而进行数据库的增删改查操作。内容涵盖连接数据库的基本步骤和相关代码示例。

被折叠的 条评论
为什么被折叠?



