首页 > 数据库 > mysql教程 > jdbc链接mysql,access和oracle例子

jdbc链接mysql,access和oracle例子

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
发布: 2016-06-07 15:34:02
原创
1157 人浏览过

一,链接 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();

           }

       }

    }

}

 

相关标签:
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
java - jdbc无法连接postgresql数据库
来自于 1970-01-01 08:00:00
0
0
0
mysql - Jdbc 链接数据库
来自于 1970-01-01 08:00:00
0
0
0
JDBC日期时间类型
来自于 1970-01-01 08:00:00
0
0
0
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板