本文实例讲述了java实现的连接oracle mysql数据库功能。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
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程序设计有所帮助。