yzt
2023-05-05 4c558c77a6a9d23f057f094c4dc3e315eabef497
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.hxzkoa.tools;
 
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;
 
public class JdbcTemplate {
    public void getUser(String url) {
        Connection con = null;// 创建一个数据库连接
        CallableStatement pre = null;// 创建预编译语句对象,一般都是用这个而不用Statement
        try {
            // Class.forName("oracle.jdbc.driver.OracleDriver");// 加载Oracle驱动程序
            Class.forName("com.mysql.jdbc.Driver");// 加载Oracle驱动程序
            System.out.println("开始尝试连接数据库!");
            // String user = "inituser";// 用户名,系统默认的账户名
            // String password = "initpass";// 你安装时选设置的密码
            String user = "root";// 用户名,系统默认的账户名
            String password = "123456";// 你安装时选设置的密码
            con = DriverManager.getConnection(url, user, password);// 获取连接
            System.out.println("连接成功!");
            String sql = "{call SYSTEM.GETCOMMONUSERPASS(?,?)}";// 预编译语句,“?”代表参数
            pre = con.prepareCall(sql);// 实例化预编译语句
            pre.registerOutParameter(1, Types.BLOB);
            pre.registerOutParameter(2, Types.BLOB);
            pre.execute();// 执行查询,注意括号中不需要再加参数
            // System.out.println(BLOB2String(pre.getBlob(1)));
            System.out.println(BLOB2String(pre.getBlob(1)));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 逐一将上面的几个对象关闭,因为不关闭的话会影响性能、并且占用资源
                // 注意关闭的顺序,最后使用的最先关闭
                if (pre != null)
                    pre.close();
                if (con != null)
                    con.close();
                System.out.println("数据库连接已关闭!");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
    public static void main(String[] args) {
        // String url="jdbc:oracle:thin:@//166.111.71.241:1521/da";
        String url = "jdbc:mysql://127.0.0.1:3306/hxzkoa?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true";
        // new JdbcTemplate().getUser(url);
        String username = "root";// 用户名,系统默认的账户名
        String password = "123456";// 你安装时选设置的密码
        tryConnect(url, username, password);
    }
 
    public static String BLOB2String(Object o) throws SQLException {
        String str = null;
        byte[] inbyte = null;
        if (o instanceof Blob) {
            Blob blob = (Blob) o;
            if (blob != null) {
                inbyte = blob.getBytes(1, (int) blob.length());
            }
            str = new String(inbyte);
        }
        return str;
    }
 
    public static Boolean tryConnect(String url, String username,
            String password) {
        Connection con = null;// 创建一个数据库连接
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");// 加载Oracle驱动程序
            System.out.println("开始尝试连接数据库!");
            con = DriverManager.getConnection(url, username, password);// 获取连接
            System.out.println("连接成功!");
            if (con != null) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("设置数据库用户、密码失败!");
        } finally {
            try {
                // 逐一将上面的几个对象关闭,因为不关闭的话会影响性能、并且占用资源
                // 注意关闭的顺序,最后使用的最先关闭
                if (con != null)
                    con.close();
                System.out.println("数据库连接已关闭!");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}