服务器之家

服务器之家 > 正文

java实现的连接oracle/mysql数据库功能简单示例【附oracle+mysql数据库驱动包】

时间:2021-01-17 15:07     来源/作者:zhangzhinuo

本文实例讲述了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程序设计有所帮助。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
返回顶部