1.首先:jdbc所需要的maven依赖
2.java获取到数据库的连接.
jdbc工具类
public final class jdbcUtil {
private static final String URL = "jdbc:mysql://120.25.227.234:3306/user?useSSL=false";
private static final String USER = "user";
private static final String PASSWORD = "root";
private static Connection connection = null;
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
connection = (Connection) DriverManager.getConnection(URL, USER, PASSWORD);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public static Connection getConnection(){
return connection;
}
}
工具类的使用方法:
public class userDaoImpl implements userDao{
//直接获取链接
Connection conn = jdbcUtil.getConnection();
public boolean getUserInstance(String username, String password) throws Exception {
String sql = "select * from user where username = ? and password = ?";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1,username);
preparedStatement.setString(2,password);
ResultSet resultSet = preparedStatement.executeQuery();
if(!resultSet.next()){
return false;
}else {
return true;
}
}
}