欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

jdbc链接mysql,access和oracle例子

程序员文章站 2022-05-20 14:00:38
...

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

}

}

}

}