這篇文章主要介紹了java實現的連接oracle/mysql數據庫功能,結合實例形式分析了java基於jdbc連接Oracle與mysql的相關操作技巧,並附帶完整實例代碼與oracle+mysql數據庫驅動包供讀者下載參考,需要的朋友可以參考下
本文實例講述了java實現的連接oracle mysql資料庫功能。分享給大家參考,具體如下:
package com.nuo.test.Connection; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DBUtil{ public static Connection getConnection() throws Exception{ Connection conn=null; try { Class.forName( //"oracle.jdbc.driver.OracleDriver" "com.mysql.jdbc.Driver" ); conn=DriverManager.getConnection( //"jdbc:oracle:thin:@127.0.0.1:1521:qiye","jossinfo","tao" "jdbc:mysql://localhost:3306/care","root","nuo" ); } catch (Exception e) { e.printStackTrace(); throw e; } return conn; } public static void close(Connection conn) throws Exception{ if(conn!=null){ try { conn.close(); } catch (SQLException e) { System.out.println(e); } } } public static void main(String[] args) throws Exception{ ResultSet rs = null; Connection conn = null; String valiresult=""; try{ conn = DBUtil.getConnection(); PreparedStatement prep = conn.prepareStatement( "select * from tab_user " ); rs = prep.executeQuery(); while(rs.next()){ valiresult=rs.getString(1);//表第一列内容 System.out.println(valiresult); } }catch(Exception e){ e.printStackTrace(); throw e; }finally{ DBUtil.close(conn); } } }
以上是Java實作連接oracle/mysql資料庫功能簡單範例的詳細內容。更多資訊請關注PHP中文網其他相關文章!