Luke的学习笔记

吃得苦中苦,方为人上人

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

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的连接写法,差别大大的,不然你死活连不上。还有端口号

阅读更多
版权声明:北京邮电大学软件学院Luke https://blog.csdn.net/u012662688/article/details/51565451
个人分类: 数据库 jdbc
上一篇C++ map 中的reverse_iterator
下一篇mysql查询某属性下所有值的前N条记录
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭