java连接sqlserver数据库代码

java,import java.sql.Connection;,import java.sql.DriverManager;,import java.sql.SQLException;,,public class JDBCTest {, public static void main(String[] args) {, String url = "jdbc:sqlserver://localhost:1433;databaseName=数据库名";, String username = "用户名";, String password = "密码";, try {, Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");, Connection connection = DriverManager.getConnection(url, username, password);, System.out.println("连接成功!");, connection.close();, } catch (ClassNotFoundException e) {, e.printStackTrace();, } catch (SQLException e) {, e.printStackTrace();, }, },},

以下是使用Java连接SQL数据库的详细代码,包括小标题和单元表格:

java连接sqlserver数据库代码

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DatabaseConnection {
    public static void main(String[] args) {
        // 加载数据库驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("数据库驱动加载失败");
            e.printStackTrace();
            return;
        }
        // 连接数据库
        String url = "jdbc:mysql://localhost:3306/数据库名";
        String username = "用户名";
        String password = "密码";
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, username, password);
            System.out.println("数据库连接成功");
        } catch (SQLException e) {
            System.out.println("数据库连接失败");
            e.printStackTrace();
            return;
        }
        // 执行查询操作
        String query = "SELECT * FROM 表名";
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            statement = connection.createStatement();
            resultSet = statement.executeQuery(query);
            while (resultSet.next()) {
                System.out.println("字段1:" + resultSet.getString("字段1") + ",字段2:" + resultSet.getString("字段2"));
            }
        } catch (SQLException e) {
            System.out.println("查询操作失败");
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (statement != null) {
                    statement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                System.out.println("关闭资源失败");
                e.printStackTrace();
            }
        }
    }
}

请将上述代码中的数据库名用户名密码表名字段1字段2替换为实际的数据库信息。

java连接sqlserver数据库代码

原创文章,作者:未希,如若转载,请注明出处:https://www.kdun.com/ask/640742.html

本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。

(0)
未希新媒体运营
上一篇 2024-05-21 08:42
下一篇 2024-05-21 08:42

相关推荐

  • java连接sqlserver代码

    “java,import java.sql.Connection;,import java.sql.DriverManager;,import java.sql.SQLException;,,public class JavaConnectSqlServer {, public static void main(String[] args) {, String url = “jdbc:sqlserver://localhost:1433;databaseName=数据库名”;, String username = “用户名”;, String password = “密码”;,, try {, Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”);, Connection connection = DriverManager.getConnection(url, username, password);, System.out.println(“连接成功”);, connection.close();, } catch (ClassNotFoundException e) {, e.printStackTrace();, } catch (SQLException e) {, e.printStackTrace();, }, },},“

    2024-05-21
    0109
  • java连接sqlserver数据库代码

    “java,import java.sql.Connection;,import java.sql.DriverManager;,import java.sql.SQLException;,,public class JDBCTest {, public static void main(String[] args) {, String url = “jdbc:sqlserver://localhost:1433;databaseName=数据库名”;, String username = “用户名”;, String password = “密码”;, try {, Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”);, Connection connection = DriverManager.getConnection(url, username, password);, System.out.println(“连接成功!”);, connection.close();, } catch (ClassNotFoundException e) {, e.printStackTrace();, } catch (SQLException e) {, e.printStackTrace();, }, },},“

    2024-05-21
    051

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

产品购买 QQ咨询 微信咨询 SEO优化
分享本页
返回顶部
云产品限时秒杀。精选云产品高防服务器,20M大带宽限量抢购 >>点击进入