zoukankan      html  css  js  c++  java
  • 【原创】大叔经验分享(94)jdbc连接mysql、sqlserver、oracle

    Mysql

    driver下载:https://mvnrepository.com/artifact/mysql/mysql-connector-java

    import java.sql.*;
    public class SqlServerTest {
             public static void main(String []args) {
             String driverName="com.mysql.jdbc.Driver";
             String dbURL="jdbc:mysql://$ip:3306/$db";
             String userName="$user";
             String userPwd="$password";
              try
            {
                Class.forName(driverName);
                Connection dbConn=DriverManager.getConnection(dbURL,userName,userPwd);
                Statement st = dbConn.createStatement();
                ResultSet rs = st.executeQuery("select * from user");
                while (rs.next()) {
                  System.out.println(rs.getString(1));
                }
                System.out.println("success!");
             }
              catch(Exception e)
           {
                e.printStackTrace();
                System.out.print("fail!");
           }
        }
     }

    SqlServer

    driver下载:https://mvnrepository.com/artifact/com.microsoft.sqlserver/sqljdbc4/4.0

    import java.sql.*;
    public class SqlServerTest {
             public static void main(String []args) {
             String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
             String dbURL="jdbc:sqlserver://$ip:1433;DatabaseName=$db";
             String userName="$user";
             String userPwd="$password";
              try
            {
                Class.forName(driverName);
                Connection dbConn=DriverManager.getConnection(dbURL,userName,userPwd);
                Statement st = dbConn.createStatement();
                ResultSet rs = st.executeQuery("select * from sysobjects where xtype='U'");
                while (rs.next()) {
                  System.out.println(rs.getString(1));
                }
                System.out.println("success!");
             }
              catch(Exception e)
           {
                e.printStackTrace();
                System.out.print("fail!");
           }
        }
     }

    Oracle

    driver下载:https://www.oracle.com/database/technologies/jdbc-drivers-12c-downloads.html

    import java.sql.*;
    public class SqlServerTest {
             public static void main(String []args) {
             String driverName="oracle.jdbc.driver.OracleDriver";
             String dbURL="jdbc:oracle:thin:@//$ip:1521/$db";
             String userName="$user";
             String userPwd="$password";
              try
            {
                Class.forName(driverName);
                Connection dbConn=DriverManager.getConnection(dbURL,userName,userPwd);
                Statement st = dbConn.createStatement();
                ResultSet rs = st.executeQuery("select * from all_tab_comments");
                while (rs.next()) {
                  System.out.println(rs.getString(1));
                }
                System.out.println("success!");
             }
              catch(Exception e)
           {
                e.printStackTrace();
                System.out.print("fail!");
           }
        }
     }
  • 相关阅读:
    php大力力 [048节] php一点支付开发资料,很散
    php大力力 [047节] 寻找程序员的方法和应用
    ECharts的简单使用
    xlsxwriter新建Excel文件-制图
    xlsxwriter新建Excel文件
    xlrd读取Excel文件
    抽奖概率互斥算法
    Redis 的 8 大数据类型!
    Django Web框架入门-博客(更新完成)
    Git学习
  • 原文地址:https://www.cnblogs.com/barneywill/p/12069149.html
Copyright © 2011-2022 走看看