java连接数据库的代码sqlserver
时间: 2023-09-25 07:06:42 浏览: 50
### 回答1:
以下是 Java 连接 SQL Server 数据库的示例代码:
```java
import java.sql.*;
public class DatabaseConnection {
public static void main(String[] args) {
try {
// 加载数据库驱动程序
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// 连接数据库
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=test";
String username = "sa";
String password = "123456";
Connection connection = DriverManager.getConnection(url, username, password);
// 执行 SQL 查询
Statement statement = connection.createStatement();
String sql = "SELECT * FROM user";
ResultSet resultSet = statement.executeQuery(sql);
// 处理查询结果
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
System.out.printf("id=%d, name=%s, age=%d\n", id, name, age);
}
// 关闭连接
resultSet.close();
statement.close();
connection.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
在上面的代码中,我们使用了 SQL Server 数据库,如果你使用的是其他数据库,只需要替换相关的驱动程序和连接参数即可。
### 回答2:
连接SQL Server数据库的Java代码如下:
```
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JdbcConnectionExample {
public static void main(String[] args) {
String url = "jdbc:sqlserver://localhost:1433;databaseName=your_database_name";
String username = "your_username";
String password = "your_password";
Connection connection = null;
try {
// 加载驱动程序
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// 建立数据库连接
connection = DriverManager.getConnection(url, username, password);
// 执行SQL查询等操作...
// ...
System.out.println("数据库连接成功!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
```
以上代码实现了通过JDBC连接SQL Server数据库。其中需要替换的部分为:
- `your_database_name`:数据库名称
- `your_username`:登录数据库的用户名
- `your_password`:登录数据库的密码
另外,还需确保已经将对应的SQL Server JDBC驱动文件添加到项目的类路径中。
### 回答3:
Java连接SQL Server数据库的代码可以使用JDBC(Java Database Connectivity)来实现。下面是一个简单的Java代码示例:
```java
import java.sql.*;
public class SQLServerConnectionExample {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 加载JDBC驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// 连接数据库
String url = "jdbc:sqlserver://localhost:1433;databaseName=your_database_name";
String username = "your_username";
String password = "your_password";
conn = DriverManager.getConnection(url, username, password);
// 创建查询语句
String sql = "SELECT * FROM your_table_name";
stmt = conn.createStatement();
// 执行查询
rs = stmt.executeQuery(sql);
// 处理查询结果
while (rs.next()) {
// 在这里获取并处理每一行的数据
String column1 = rs.getString("column1");
int column2 = rs.getInt("column2");
// ...
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭资源
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
以上代码首先加载了SQL Server的JDBC驱动,然后创建了连接字符串,包括数据库地址、端口、数据库名、用户名和密码等信息。通过`DriverManager.getConnection()`方法连接数据库并创建一个`Statement`对象。然后执行SQL查询语句,并通过`ResultSet`对象获取查询结果。最后,关闭所有资源以释放数据库连接。
请注意,在实际使用中,你需要根据自己的数据库配置和需要执行的SQL语句进行相应的修改。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)