怎么用java连接sqlserver数据库

 我来答
不落的太阳Sean
2016-12-13 · TA获得超过281个赞
知道小有建树答主
回答量:220
采纳率:0%
帮助的人:173万
展开全部
  1. 导入SqlServer JDBC的驱动,

  2. SQLServer的JDBC URL=

 jdbc:sqlserver://172.30.202.21:1433;DatabaseName=AirAutoMonitor    

   3. 获得连接的代码

public static Connection getConnection(String url, String username, String password)
throws ResourceDirectoryException {
Connection conn = null;
String driverName = "";

Properties props = new Properties();
props.put("user", username);
props.put("password", password);

if (url != null || !"".equals(url)) {
if (url.indexOf("oracle") > -1) {
databaseType = "oracle";
props.put("remarksReporting", "true");
driverName = "oracle.jdbc.driver.OracleDriver";
}
if (url.indexOf("sqlserver") > -1) {
databaseType = "sqlserver";
driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
}
if (url.indexOf("mysql") > -1) {
databaseType = "mysql";
driverName = "com.mysql.jdbc.Driver";
}
}
try {
Class.forName(driverName);
conn = DriverManager.getConnection(url, props);
} catch (ClassNotFoundException e) {
throw new ResourceDirectoryException(e);
} catch (SQLException e) {
throw new ResourceDirectoryException(e);
}
return conn;
}

上面的代码是获得Oracle, MySQL, SqlServer的数据库连接的通用方法。

碧茂科技
2023-10-18 广告
作为上海碧茂信息科技有限公司的工作人员,对于Hadoop集群中小文件过多的问题,可以采取以下处理方法:1. 使用Hadoop Archive(HAR)文件技术。HAR是一种Hadoop文件系统中的归档格式,可将小文件组合成一个大文件,从而降... 点击进入详情页
本回答由碧茂科技提供
大学生社会实践在行动
2016-12-13 · TA获得超过707个赞
知道小有建树答主
回答量:537
采纳率:80%
帮助的人:115万
展开全部
import java.sql.Connection;    
import java.sql.DriverManager;    
import java.sql.ResultSet;    
import java.sql.Statement;    
    
public class Test {    
    
    public static void main(String args[]) {    
        // Create a variable for the connection string.    
        String connectionUrl = "jdbc:sqlserver://localhost:1433;"    
                + "databaseName=AdventureWorks;integratedSecurity=true;";    
    
        String url = "jdbc:sqlserver://127.0.0.1:1368;databaseName=mydb;user=sa;password=qiaoning";//sa身份连接    
    
        String url2 = "jdbc:sqlserver://127.0.0.1:1368;databaseName=mydb;integratedSecurity=true;";//windows集成模式连接    
    
        // Declare the JDBC objects.    
        Connection con = null;    
        Statement stmt = null;    
        ResultSet rs = null;    
    
        try {    
            // Establish the connection.    
            System.out.println("begin.");    
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");    
            con = DriverManager.getConnection(url);    
            System.out.println("end.");    
    
            // Create and execute an SQL statement that returns some data.    
            String SQL = "SELECT TOP 10 * FROM aud_t_basis";    
            stmt = con.createStatement();    
            rs = stmt.executeQuery(SQL);    
    
            // Iterate through the data in the result set and display it.    
            while (rs.next()) {    
                System.out.println(rs.getString(4) + " " + rs.getString(6));    
            }    
        }    
    
        // Handle any errors that may have occurred.    
        catch (Exception e) {    
            e.printStackTrace();    
        }    
    
        finally {    
            if (rs != null)    
                try {    
                    rs.close();    
                } catch (Exception e) {    
                }    
            if (stmt != null)    
                try {    
                    stmt.close();    
                } catch (Exception e) {    
                }    
            if (con != null)    
                try {    
                    con.close();    
                } catch (Exception e) {    
                }    
        }    
    }    
}


谢谢采纳。

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式