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!");
           }
        }
     }
  • 相关阅读:
    gdb调试core文件
    设计模式之工厂模式
    设计模式之简单工厂模式
    正确理解python的装饰器
    深入理解MVC架构
    django的模板系统过滤器笔记
    python net-snmp 的使用
    用django写个CMS系统
    django的CMS系统(内容管理系统)
    RESTful 的通俗解释
  • 原文地址:https://www.cnblogs.com/barneywill/p/12069149.html
Copyright © 2011-2022 走看看