zoukankan      html  css  js  c++  java
  • Java JDBC连接MySQL、Oracle、SQLServer数据库

    JDBC链接MySQL

        @Test
        public void mySqlTest() {
            String driverName = "com.mysql.jdbc.Driver";
            String dbUrl = "jdbc:mysql://localhost:3306/dbName";
            String username = "root";
            String password = "root";
            Connection con = null;
            try {
                Class.forName(driverName);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            try {
                con = DriverManager.getConnection(dbUrl, username, password);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            System.out.println(con.toString());
        }

    JDBC链接Oracle

        @Test
        public void oracleTest() {
            String driverName = "oracle.jdbc.driver.OracleDriver";
            String dbUrl = "jdbc:oracle:thin:@127.0.0.1:1521:服务名";
            String username = "root";
            String password = "root";
            Connection con = null;
            try {
                Class.forName(driverName);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            try {
                con = DriverManager.getConnection(dbUrl, username, password);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            System.out.println(con.toString());
        }

    JDBC链接SQLServer

        @Test
        public void sqlServerTest() {
            String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
            String dbUrl = "jdbc:sqlserver://localhost:1433;databaseName=master;";
            String username = "root";
            String password = "root";
            Connection con = null;
            try {
                Class.forName(driverName);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            try {
                con = DriverManager.getConnection(dbUrl, username, password);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            System.out.println(con.toString());
        }

     记得下载数据库驱动jar,SQLServer和Oracle jar包maven私服是没有的,自己去网上找。

  • 相关阅读:
    SQL面试积累
    The server does not support version 3.0 of the J2EE Web module specification
    AspectJ报错:error at ::0 can't find referenced pointcut XXX
    maven教程
    sqlMapConfig.xml配置文件详解
    解决eclipse+MAVEN提示One or more constraints have not been satisfied.的问题
    maven常见问题
    Dmaven.multiModuleProjectDirectory system propery is not set.
    maven最齐全配置pom.xml
    serialVersionUID的作用
  • 原文地址:https://www.cnblogs.com/mxh-java/p/12652110.html
Copyright © 2011-2022 走看看