这篇文章主要介绍了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中文网其他相关文章!