JDBC连接数据库(三种不同的数据库)(Oracle、SQLServer、MySQL)

JDBC编程步骤:

1. load the Driver

    1. Class.forName()|Class.forName().newInstance()|newDriverName()

    2.实例化时自动向DriverManager注册,不需显式调用DriverManager,registerDriver方法

2.Connect to the DataBase

    1.DriverManager.getConnection()

3.Execute the SQL

    1.Connection.CreateStatement()

    2.Statement.executeQuery()

    3.Statement.executeUpdate()

4.Retrieve the result date

    1.循环取得结果while(rs.next)

5.Show the result data

    1.将数据库种的各种类型转换为JAVA中的类型(getXXX)方法

6.Close

    1.close the resultset./close the statement./close the connection



1.JDBC连接Oracle数据库示例:




import java.sql.*;

public class TestJDBC {



   public static void main(String[] args) {

      ResultSet rs=null;

      Statement stmt=null;

      Connection conn=null;

      try{

               Class.forName("oracle.jdbc.driver.OracleDriver");

               //new oracle.jdbc.driver.OracleDriver();     

               conn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.1:1521:SXT", "scott", "tiger");

            //请大家将上面的IP地址改为自己本机的IP地址;1521是端口号,大多数都是如此,SXT是数据库名称,改为自己的数据库名词,scott是用户名,tiger是密码。

          stmt=conn.createStatement();

        rs = stmt.executeQuery("select * from Stu_Info");

        while(rs.next()){

           System.out.println(rs.getString("StuNo"));

           System.out.println(rs.getString("StuName"));

           System.out.println(rs.getString("StuSex"));

           System.out.println(rs.getString("StuAge"));

           System.out.println(rs.getString("StuAddress"));

        }

      }catch(ClassNotFoundException e){

        e.printStackTrace();

      }

      catch(SQLException e){

        e.printStackTrace();

      }finally{

        try{

             if(rs!=null){

             rs.close();

             rs=null;

           }

           if(stmt!=null){

              stmt.close();

              stmt=null;

           }

           if(conn!=null){

              conn.close();

              conn=null;

           }

        }catch(SQLException e){

           e.printStackTrace();

        }

          }

    }

}







2.JDBC连接SQLServer数据库示例:



import java.sql.*;

public class TestJDBC {



   public static void main(String[] args) {

      ResultSet rs=null;

      Statement stmt=null;

      Connection conn=null;

      try{

        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");     conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433; DatabaseName=DBDEMO", "sa","123456");

        // 与Oracle数据库类似首先加载驱动;上面1433为端口号,sa为用户名,123456为用户密码;

        stmt=conn.createStatement();

          rs = stmt.executeQuery("select * from Stu_Info");

        while(rs.next()){

           System.out.println(rs.getString("StuNo"));

           System.out.println(rs.getString("StuName"));

           System.out.println(rs.getString("StuSex"));

           System.out.println(rs.getString("StuAge"));

           System.out.println(rs.getString("StuAddress"));

        }

      }catch(ClassNotFoundException e){

        e.printStackTrace();

      }

      catch(SQLException e){

        e.printStackTrace();

      }finally{

        try{

             if(rs!=null){

             rs.close();

             rs=null;

           }

           if(stmt!=null){

              stmt.close();

              stmt=null;

           }

           if(conn!=null){

              conn.close();

              conn=null;

           }

        }catch(SQLException e){

           e.printStackTrace();

        }

          }

    }

}



3.JDBC连接MySql示例:


import java.sql.*;

public class TestJDBC {



   public static void main(String[] args) {

      ResultSet rs=null;

      Statement stmt=null;

      Connection conn=null;

      try{

        Class.forName("com.mysql.jdbc.SQLServerDriver");     conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/DBDEMO", "root","root");

        // 与上面数据库类似首先加载驱动;上面3306为端口号,DBDEMO为数据库,root为用户名,root为用户密码;

        stmt=conn.createStatement();

          rs = stmt.executeQuery("select * from Stu_Info");

        while(rs.next()){

           System.out.println(rs.getString("StuNo"));

           System.out.println(rs.getString("StuName"));

           System.out.println(rs.getString("StuSex"));

           System.out.println(rs.getString("StuAge"));

           System.out.println(rs.getString("StuAddress"));

        }

      }catch(ClassNotFoundException e){

        e.printStackTrace();

      }

      catch(SQLException e){

        e.printStackTrace();

      }finally{

        try{

             if(rs!=null){

             rs.close();

             rs=null;

           }

           if(stmt!=null){

              stmt.close();

              stmt=null;

           }

           if(conn!=null){

              conn.close();

              conn=null;

           }

        }catch(SQLException e){

           e.printStackTrace();

        }

          }

    }

}
以上所用的数据库是自己建的一个数据库,名称为DBDEMO,里面定义了stuNo,stuName,stuSex,stuAge,stuAddresss,大家可以根据自己的需要用其他的数据库进行连接。

你可能感兴趣的:(oracle,sql,编程,mysql,jdbc)