zoukankan      html  css  js  c++  java
  • JDBC连接数据库

    一、导入数据库驱动到目录

    二、添加驱动到库

    三、连接数据库

    方式一:

    public static void main(String[] args) throws SQLException {
            Driver driver = new com.mysql.jdbc.Driver();
            String url = "jdbc:mysql://localhost:3306/baizhan?useSSL=false";
            Properties info = new Properties();
            info.setProperty("user","root");
            info.setProperty("password","1234");
            Connection conn = driver.connect(url,info);
            System.out.println(conn);
        }

    方式二:

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, NoSuchMethodException, InvocationTargetException {
            Class clazz = Class.forName("com.mysql.jdbc.Driver");
            Driver driver = (Driver)clazz.getDeclaredConstructor().newInstance();
            String url = "jdbc:mysql://localhost:3306/baizhan?useSSL=false";
            Properties info = new Properties();
            info.setProperty("user","root");
            info.setProperty("password","1234");
            Connection conn = driver.connect(url,info);
            System.out.println(conn);
        }

    方式三:

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, NoSuchMethodException, InvocationTargetException {
            Class clazz = Class.forName("com.mysql.jdbc.Driver");
            Driver driver = (Driver)clazz.getDeclaredConstructor().newInstance();
            DriverManager.registerDriver(driver);
            String url = "jdbc:mysql://localhost:3306/baizhan?useSSL=false";
            String user = "root";
            String password = "1234";
            Connection conn = DriverManager.getConnection(url,user,password);
            System.out.println(conn);
        }

    方式四:

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, NoSuchMethodException, InvocationTargetException {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/baizhan?useSSL=false";
            String user = "root";
            String password = "1234";
            Connection conn = DriverManager.getConnection(url,user,password);
            System.out.println(conn);
        }

    方式五

    创建properties文件

    user=root
    password=1234
    url=jdbc:mysql://localhost:3306/baizhan?useSSL=false
    driver=com.mysql.jdbc.Driver
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, NoSuchMethodException, InvocationTargetException, IOException {
            InputStream is = Jdbc_test.class.getClassLoader().getResourceAsStream("jdbc.propertise");
            Properties properties = new Properties();
            properties.load(is);
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String url = properties.getProperty("url");
            String driver = properties.getProperty("driver");
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url,user,password);
            System.out.println(conn);
        }
  • 相关阅读:
    【网易官方】极客战记(codecombat)攻略-森林-加农炮之舞forest-cannon-dancing
    【网易官方】极客战记(codecombat)攻略-森林-森林慢跑forest-jogging
    https://developer.android.com/codelabs/java-to-kotlin
    今日英语
    架构师技能图谱
    java接口防重提交如何处理
    看看人家那后端API接口写得,那叫一个优雅!
    MySQL不推荐使用uuid或者雪花id作为主键
    “12306”是如何支撑百万QPS的?
    阿里巴巴为什么能抗住90秒100亿?看完这篇你就明白了!
  • 原文地址:https://www.cnblogs.com/baisha/p/15463171.html
Copyright © 2011-2022 走看看