本文实例讲述了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中文网其它相关文章!