关闭

java用jdbc连接mysql数据库和sql server数据库

标签: javajdbcsql servermysql数据库
340人阅读 评论(0) 收藏 举报
分类:
public static void cre_mysql(String lst,String term,String kind) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url_2 = "jdbc:mysql://" + tar_URL + "/" + tar_dbName + "?useUnicode=true&characterEncoding=utf-8";
            Connection conn = DriverManager.getConnection(url_2, tar_username, tar_password);
            Statement stmt = conn.createStatement();
	        stmt.executeUpdate(exec);
	        
            stmt.close();
            conn.close();
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
    }


public static  void con_sqlserv(String lst,String term,String kind) {
        try {
	    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String url_1 = "jdbc:sqlserver://" + src_URL + ";DatabaseName=" + src_dbName;
            Connection conn = DriverManager.getConnection(url_1, src_username, src_password);
            //System.out.println("here");
            Statement stmt = conn.createStatement();
            String exec="select " +tot_term+" from dbo.\""+lst+"\"";
            System.out.println(exec);
            ResultSet rs = stmt.executeQuery(exec);
            tmp=0;
            int col = rs.getMetaData().getColumnCount();
            while (rs.next()) {
            	sav[tmp]="(";
                for (int i = 1; i <= col; i++) {
                	String tmp_rs=rs.getString(i);
                	if(kind_set[i-1].indexOf("varchar")!=-1||kind_set[i-1].indexOf("datetime")!=-1)
                		tmp_rs="'"+tmp_rs+"'";
                    if(i!=col)
                    	sav[tmp]+=tmp_rs+",";
                    else 
                    	sav[tmp]+=tmp_rs+")";
                }
                //System.out.println(sav[tmp]);
            	tmp++;
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
    }


注意区分mysql和sql_server的连接写法,差别大大的,不然你死活连不上。还有端口号

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:63298次
    • 积分:1513
    • 等级:
    • 排名:千里之外
    • 原创:85篇
    • 转载:4篇
    • 译文:4篇
    • 评论:18条
    最新评论