• JDBC入门、数据库连接方式


    JDBC程序编写步骤

    1.注册驱动(加载Driver类)

    2.获取连接(得到Connection)

    3.执行增删改查(发送SQL给mysql执行)

    4.释放资源(关闭相关连接)

    数据库连接的5种方式

    方式一:

    package chapter01;
    ​
    //第一个JDBC程序,完成简单的操作
    //连接数据库方式一
    import com.mysql.jdbc.Driver;
    ​
    import java.sql.Connection;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;
    ​
    public class JDBC01 {
        public static void main(String[] args) throws SQLException {
    ​
            //1.注册驱动
            //Driver driver = new Driver();
            com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver();
    ​
            //2.得到连接
            /*
            *1.jdbc:mysql(规定好表示协议,通过jdbc的方式连接mysql)
            *2.localhost(主机,可以是ip地址)
            *3.3306(表示mysql监听的端口)
            *4.myhan(连接到mysql的哪一个数据库)
            *5.mysql的连接本质就是java中socket连接
            * */
    ​
            String url = "jdbc:mysql://localhost:3306/myhan?        &useSSL=false&serverTimezone=UTC";
    ​
            //
            //将用户名和密码放入到Properties对象(user和password是规定好的)
            Properties properties = new Properties();
            properties.setProperty("user","root");//用户
            properties.setProperty("password","abc123");//密码
            //网络连接
            Connection connect = driver.connect(url, properties);
    ​
            //3.执行sql语句
            String sql = "insert into jh_db02 values(null,'tom','2011-12-15','12345','男')";
            //Statement用于执行静态SQL语句并返回其生成的结果对象
            Statement statement = connect.createStatement();
            int rows = statement.executeUpdate(sql);//若是dml语句,则返回的是影响行数
    ​
            System.out.println(rows > 0 ? "成功" : "失败");
    ​
            //4.关闭连接
            statement.close();
            connect.close();
    ​
    ​
    ​
    ​
        }
    }

    方式二:

    package chapter01;
    ​
    import java.sql.Connection;
    import java.sql.Driver;
    import java.sql.SQLException;
    import java.util.Properties;
    ​
    //连接数据库方式二
    ​
    public class JDBC02 {
        public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
    ​
            //使用反射加载Driver类,动态加载,更加的灵活,减少依赖性
    ​
            //注册驱动
            String path = "com.mysql.cj.jdbc.Driver";
            Class class01 = Class.forName(path);
            Driver driver = (Driver)class01.newInstance();
    ​
            //得到连接
            String url = "jdbc:mysql://localhost:3306/myhan?&useSSL=false&serverTimezone=UTC";
            Properties properties = new Properties();
            properties.setProperty("user","root");
            properties.setProperty("password","abc123");
    ​
            Connection connect = driver.connect(url, properties);
            System.out.println(connect);
    ​
            //执行sql语句
    ​
    ​
            //关闭连接
            connect.close();
    ​
    ​
        }
    }

    方式三:

    package chapter01;
    ​
    
    //连接数据库方式三
    ​
    import java.sql.Connection;
    import java.sql.Driver;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.Properties;
    ​
    public class JDBC03 {
        public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
    ​
            //使用DriverManager 替代 Driver进行统一管理
            String path = "com.mysql.cj.jdbc.Driver";
            Class aClass = Class.forName(path);
            Driver driver = (Driver) aClass.newInstance();
    ​
            String url = "jdbc:mysql://localhost:3306/myhan?&useSSL=false&serverTimezone=UTC";
            String uesr = "root";
            String password = "abc123";
    ​
            DriverManager.deregisterDriver(driver);//注册驱动
            Connection connection = DriverManager.getConnection(url, uesr, password);
    ​
            System.out.println(connection);
    ​
    ​
        }
    }

    方式四:

    package chapter01;
    
    //该方式使用最多,有静态代码块已经自动完成注册驱动
    ​
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    ​
    public class JDBC04 {
        public static void main(String[] args) throws ClassNotFoundException, SQLException, SQLException {
    ​
            //使用Class.forName自动完成注册驱动
            String path = "com.mysql.jdbc.Driver";
            Class class01 = Class.forName(path);
            String url = "jdbc:mysql://localhost:3306/myhan";
            String user = "root";
            String password = "abc123";
    ​
            Connection connection = DriverManager.getConnection(url, user, password);
            System.out.println(connection);
    ​
    ​
        }
    }

    方式五:

    配置文件:

    user=root
    password=abc123
    url=jdbc:mysql://localhost:3306/myhan
    driver=com.mysql.jdbc.Driver

    代码文件:

    ​
    package chapter01;
    ​
    //在方式四的基础上,增加配置文件,使得mysql更加灵活
    ​
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.Properties;
    ​
    public class JDBC05 {
        public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
    ​
            //通过properites对象获取配置文件信息
            Properties properties = new Properties();
            properties.load(new FileInputStream("src\\mysql.properties"));
            //获取相关的值
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String driver = properties.getProperty("driver");
            String url = properties.getProperty("url");
    ​
            Class.forName(driver);
            Connection connection = DriverManager.getConnection(url, user, password);
            System.out.println(connection);
    ​
        }
    }

  • 相关阅读:
    造轮子之消息实时推送
    LeaRun.net快速开发动态表单
    NetCore/Net8下使用Redis的分布式锁实现秒杀功能
    Python的PyQt框架的使用-创建主窗体篇
    配置Maven环境
    今天步行数4000
    mvcc机制中的快照读和当前读
    为什么说制造企业需要部署MES管理系统
    this指向
    基于SSM+Vue的在线作业管理系统的设计与实现
  • 原文地址:https://blog.csdn.net/weixin_46065214/article/details/126250024