jdbc连接 SQLserver
时间: 2023-09-27 12:03:57 浏览: 43
可以使用以下代码连接SQL Server数据库:
```
import java.sql.*;
public class JdbcSQLServerExample {
public static void main(String[] args) {
// Create a variable for the connection string.
String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=AdventureWorks;user=UserName;password=*****";
try (Connection con = DriverManager.getConnection(connectionUrl);
Statement stmt = con.createStatement();) {
String SQL = "SELECT TOP 10 * FROM Person.Contact";
ResultSet rs = stmt.executeQuery(SQL);
// Iterate through the data in the result set and display it.
while (rs.next()) {
System.out.println(rs.getString("FirstName") + " " + rs.getString("LastName"));
}
}
// Handle any errors that may have occurred.
catch (SQLException e) {
e.printStackTrace();
}
}
}
```
请注意,您需要将connectionUrl, databaseName, user 和 password更改为您自己的实际值。