相关文章推荐

二、准备数据库连接

启动数据库服务,查看并确认数据库地址:这里注意数据库名不能弄错。填入已经准备好的假数据。

在这里插入图片描述
在scr文件夹中添加jdbc.properties文件,用于存放数据库的路径,用户名,密码等信息。如下:

url=jdbc:mysql://localhost:3306/sqltest?useUnicode=true&characterEncoding=utf-8
user=root
password=123456
drive=com.mysql.jdbc.Driver

三、编写获取数据库连接的类

知道了数据库信息,有了举动包之后,就可以在程序中获取数据库连接
1)获取数据库的属性:

 Properties properties = new Properties();
 ClassLoader classLoader = JDBCUtils.class.getClassLoader();
 URL res = classLoader.getResource("jdbc.properties");

2)读取属性文件中的信息

 properties.load(new FileReader(path));
 url = properties.getProperty("url");
 user = properties.getProperty("user");
 password = properties.getProperty("password");
 drive = properties.getProperty("drive");
 Class.forName(drive);

3)编写连接数据库方法:

 public static Connection getconnection() throws SQLException {
        return DriverManager.getConnection(url,user,password);

4)编写关闭连接的方法:

   public static void close(ResultSet rs, Statement stmt,Connection conn) {
        try {
            if (rs != null) {
                rs.close();
            if (stmt != null) {
                stmt.close();
            if (conn != null) {
                conn.close();
        } catch (SQLException e) {
            e.printStackTrace();

四、编写登录服务方法:

获取连接之后,从数据库中读取对象,比较其密码和用户输入的密码是否一致。

 public boolean login(String username ,String password){
        if(username==null||password==null){return false;}
        Connection connection=null;
        Statement statement=null;
        ResultSet resultSet =null;
        try {
            connection=JDBCUtils.getconnection();
            String sql = " select * from jdbctable where name='"+username+"' and password = '"+password+"'";
            statement=connection.createStatement();
            resultSet=statement.executeQuery(sql);
            return resultSet.next();
        } catch (SQLException e) {
           e.printStackTrace();
        finally {JDBCUtils.close(resultSet,statement,connection);
        return false;

接下来在主函数中调用login方法:

public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Scanner sc =new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username=sc.nextLine();
        System.out.println("请输入密码:");
        String password=sc.nextLine();
        boolean loginFlag = new jdbc_test().login(username,password);
        if(loginFlag){
            System.out.println("登录成功");
        else {
            System.out.println("登录失败");

五、测试连接

直接运行程序,输入用户名和密码:
在这里插入图片描述
输错密码:
在这里插入图片描述
输入不存在的用户:
在这里插入图片描述

将该类加载到内存,static静态代码块会在该.class被加载的时候执行.静态代码块(static block)在类加载过程中执行,且只执行一次。上面理论上来说应该是preparedStatement.setInt setDouble等等,但是测试,setString也是可以的.但是如果数据库是int 你setString(1,"haha") 那么会报错,报错原因是 'haha'无法转换为int。主要是Class.forName("com.mysql.cj.jdbc.Driver"); JDBC是一组用于执行SQL语句的Java API,它为开发人员提供了统一的编程接口,以实现不同数据库之间的互操作性。通过JDBC,开发者可以轻松地在Java应用程序中集成MySQL数据库。JDBC URL作为连接MySQL数据库的关键组件,其正确的配置对于保证应用的稳定运行至关重要。通过本文的详尽解析,希望读者能深入理解JDBC URL的结构与参数意义,掌握在不同场景下的最佳实践。 但凡涉及到数据库的应用,基本上都离不开数据库连接池工具的使用,但在使用时,对于应用而言,无需关注你获取到的连接是否可用,不像之前,有个著名的MySQL连接8小时问题,现在根本不用关心从连接池拿到的连接是否可用,你拿到了说明就是可用的,这一点是数据库连接池的框架保证的,常见的数据库连接池都有保证,今天主要探讨下SpringBoot默认的数据库连接池HikariCP和Durid连接池在连接的有效性探测的差异。
 
推荐文章