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;
}
}
}