Administrator
2021-12-14 2e05579d876b10f5292b3a6ee537cbe0db0470ab
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.sql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.GregorianCalendar;
import java.util.Properties;
 
import com.base.Com;
import com.config.AppConfig;
 
public class Sql_Sybase 
{
    public final static int SQL_TYPE_SYBASE_DEMO = 0;
    public final static int SQL_TYPE_SYBASE = 1;
    public final static int SQL_TYPE_MSSQLSERVER = 2;
    private int m_SQL_TYPE = SQL_TYPE_SYBASE_DEMO;
    //´´½¨Êý¾Ý¿âÁ¬½Ó³Ø
    //private ComboPooledDataSource sybase_ds = new ComboPooledDataSource();
    
    private String mSQLServerIp = "127.0.0.1";
    private String mSQLServerUsrName = "sxfg001";
    private String mSQLServerPWD = "sxfg001";
    private int mSQLServerPort = 5000;
    
    private ResultSet sql_res = null;
    private Connection sql_conn = null;
    
    public Sql_Sybase(AppConfig cfg)
    {
        m_SQL_TYPE = cfg.getSourceSQLServerType();
        mSQLServerIp = cfg.getSourceSQLServerIp();
        mSQLServerUsrName = cfg.getSourceSQLServerUsrName();
        mSQLServerPWD = cfg.getSourceSQLServerPWD();
        mSQLServerPort = cfg.getSourceSQLServerPort();
        
        sql_conn = getConnection();
    }
    
    public int get_SQL_TYPE()
    {
        return m_SQL_TYPE;
    }
    
    
    
    
    
    
    public Connection getConnection(){
        Connection con = null;
        if((SQL_TYPE_SYBASE_DEMO == m_SQL_TYPE) || (SQL_TYPE_SYBASE == m_SQL_TYPE))
        {
            try {
                Class.forName("com.sybase.jdbc3.jdbc.SybDriver").newInstance();
                String url = "jdbc:sybase:Tds:" + mSQLServerIp + ":" + mSQLServerPort + "/SiteWeb"; 
                Properties sysProps = System.getProperties();
                sysProps.put("user", mSQLServerUsrName);
                sysProps.put("password", mSQLServerPWD);
                
                con = DriverManager.getConnection(url, sysProps);
            } catch (InstantiationException | IllegalAccessException
                    | ClassNotFoundException | SQLException e) {
                //e.printStackTrace();
                //System.err.println(e.getMessage());
            }
        }
        else if(SQL_TYPE_MSSQLSERVER == m_SQL_TYPE)
        {
            try {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                String url = "jdbc:sqlserver://" + mSQLServerIp + ";DatabaseName=SiteWeb";
                /*
                String userName = "fuguang";
                String userPwd = "jian(12)";
                */
                con = DriverManager.getConnection(url, mSQLServerUsrName, mSQLServerPWD);
            } catch (ClassNotFoundException | SQLException e) {
                //e.printStackTrace();
            }
        }
        
        return con;
    }
    
    
    public ResultSet getSqlres()
    {
        return sql_res;
    }
    public void close_conn() throws SQLException
    {
        if(null != sql_conn)
        {
            sql_conn.close();
            sql_conn = null;
        }
    }
    public boolean get_conn_state()
    {
        if(null == sql_conn)
            return false;
        else return true;
    }
    
    /**
     * ¸ù¾Ý²éѯÓï¾ä»ñÈ¡ ResultSet ¼¯ºÏ
     * @param sql_str ²éѯÓï¾ä
     * @return            ResultSet¼¯ºÏ
     * @throws SQLException
     */
    public ResultSet sqlSybaseQuery(String sql_str) throws SQLException 
    {
        Statement sql = sql_conn.createStatement();
        sql.setQueryTimeout(30);
        ResultSet rs = sql.executeQuery(sql_str);
        return rs;
    }
    
    public void sqlSybaseExecute(String str) throws SQLException 
    {
        Statement sql = sql_conn.createStatement();
        sql.setQueryTimeout(30);
        sql.execute(str);
    }
}