怎么用java连接sqlserver数据库
发布网友
发布时间:2022-04-23 15:11
我来回答
共3个回答
懂视网
时间:2022-04-09 22:06
<%@ page language="java" import="java.sql.*" import="java.io.*" import="java.util.*" pageEncoding="utf-8"%>
2 <%
3 String path = request.getContextPath();
4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
5 %>
6
7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
8 <html>
9 <head>
10 <base href="<%=basePath%>">
11
12 <title></title>
13 <meta http-equiv="pragma" content="no-cache">
14 <meta http-equiv="cache-control" content="no-cache">
15 <meta http-equiv="expires" content="0">
16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17 <meta http-equiv="description" content="This is my page">
18 </head>
19 <%
20
21 Statement sql;
22 ResultSet rs;
23 String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动
24 String dbURL = "jdbc:sqlserver://172.16.0.5:1433; DatabaseName=Web"; //连接服务器和数据库
25 String userName = "sa"; //用户名
26 String userPwd = "Htwy*99"; //密码
27 Connection dbConn;
28
29 try {
30 Class.forName(driverName);
31 dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
32 sql=dbConn.createStatement();
33 rs=sql.executeQuery("select*from dbo.StaffBill");
34 System.out.println("Connection Successful!"); //如果连接成功 控制台输出Connection Successful!
35
36 out.println("EmployeeID");
37
38
39 while(rs.next())
40 {
41
42 out.println("<br/>");
43 out.println(rs.getString(1));
44
45 }
46
47 dbConn.close();
48 } catch (Exception e) {
49 e.printStackTrace();
50 }
51 %>
52 </body>
53 </html>
java链接sqlserver数据库
标签:ros use style except content 服务 cep his stat
热心网友
时间:2022-04-09 19:14
导入SqlServer JDBC的驱动,
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的数据库连接的通用方法。
热心网友
时间:2022-04-09 20:32
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) {
}
}
}
}
谢谢采纳。