Home > Database > Mysql Tutorial > jdbc链接mysql,access和oracle例子

jdbc链接mysql,access和oracle例子

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 15:34:02
Original
1162 people have browsed it

一,链接 mysql: Class. forName ( "com.mysql.jdbc.Driver" ); Connection conn = DriverManager . getConnection ( "jdbc:mysql://localhost/mydata?user=rootpassword=6656565" ); Statement stmt = conn.createStatement(); ResultSet rs = stmt.execute

一,链接mysql:

Class.forName("com.mysql.jdbc.Driver");

           Connection conn = DriverManager        .getConnection("jdbc:mysql://localhost/mydata?user=root&password=6656565");

           Statement stmt = conn.createStatement();

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

 

例子:

import java.sql.*;

publicclass TestMySqlConnect {

    publicstaticvoid main(String[] args) {

       Connection conn = null;

       Statement stmt = null;

       ResultSet rs = null;

       try {

           Class.forName("com.mysql.jdbc.Driver");

           conn = DriverManager

    .getConnection("jdbc:mysql://localhost/mydata?user=root&password=6656565");

           stmt = conn.createStatement();

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

           while (rs.next()) {

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

           }

       } catch (ClassNotFoundException e) {

           e.printStackTrace();

       } catch (SQLException e) {

           e.printStackTrace();

       } finally {

           try {

              if (conn != null) {

                  conn.close();conn = null;

              }

              if (stmt != null) {

                  stmt.close();stmt = null;

              }

              if (rs != null) {

                  rs.close();rs = null;

              }

           } catch (SQLException e) {

              e.printStackTrace();

           }

       }

    }

}


二,链接access

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

           Connection conn = DriverManager    .getConnection("jdbc:odbc:TestConnectToJava");

           Statement stmt = conn.createStatement();

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

例子:

import java.io.*;

import java.sql.*;

publicclass TestAccessConnect 

{

    publicstaticvoid main(String[] args)

    {

       Connection conn=null;

       Statement stmt=null;

       ResultSet rs=null;

       try{

           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

           conn=DriverManager.getConnection("jdbc:odbc:TestConnectToJava");

           stmt=conn.createStatement();

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

           while (rs.next()){

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

           }

       } 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();

           }

       }

    }

}


三,链接oracle

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

           conn=DriverManager

    .getConnection("jdbc:oracle:thin:@192.168.1.103:1521:ORCL",user,password);

           stmt=conn.createStatement();

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

 

例子:

import java.sql.*;

publicclass TestOracleConnect {

    publicstaticvoid main(String [] args){

       Connection conn=null;

       Statement stmt=null;

       ResultSet rs=null;

       try {

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

    conn=DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.103:1521:ORCL","huanran","huanran");

           stmt=conn.createStatement();

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

           while (rs.next()){

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

           }         

       } 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();

           }

       }

    }

}

 

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template