返回顶部

收藏

JDBC连接MySql和Sql Server代码

更多
Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://" + ip + ":3306/"
                    + dbName, user, pwd);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

sqlserver

    // 得到数据库连接
    public Connection getConn(String server, String dbname, String u, String pwd)
            throws Exception {
        String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String URL = "jdbc:sqlserver://" + server + ":1433" + ";DatabaseName="
                + dbname;
        String user = u;
        String password = pwd;
        System.out.println(URL);
        // 注册驱动
        Class.forName(driver).newInstance();
        // 获得数据库连接
        conn = DriverManager.getConnection(URL, user, password);
        return conn;
    }

标签:JDBC,MySql,Sql,Server

收藏

0人收藏

支持

0

反对

0