步骤 3:使用 Java 连接到 SQL - JDBC Driver for SQL Server

步骤 3:使用 Java 连接到 SQL - JDBC Driver for SQL Server

应只将此示例视为概念证明。 为了清楚起见,此示例代码已经过简化,并不一定代表 Microsoft 建议的最佳做法。

步骤 1:连接

使用 connection 类连接到 SQL 数据库。

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

public class SQLDatabaseConnection {

// Connect to your database.

// Replace server name, username, and password with your credentials

public static void main(String[] args) {

String connectionUrl =

"jdbc:sqlserver://yourserver.database.windows.net:1433;"

+ "database=AdventureWorks;"

+ "user=yourusername@yourserver;"

+ "password=;"

+ "encrypt=true;"

+ "trustServerCertificate=false;"

+ "loginTimeout=30;";

try (Connection connection = DriverManager.getConnection(connectionUrl);) {

// Code here.

}

// Handle any errors that may have occurred.

catch (SQLException e) {

e.printStackTrace();

}

}

}

步骤 2:执行查询

在此示例中,连接到 Azure SQL 数据库,执行 SELECT 语句,并返回选定行。

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class SQLDatabaseConnection {

// Connect to your database.

// Replace server name, username, and password with your credentials

public static void main(String[] args) {

String connectionUrl =

"jdbc:sqlserver://yourserver.database.windows.net:1433;"

+ "database=AdventureWorks;"

+ "user=yourusername@yourserver;"

+ "password=;"

+ "encrypt=true;"

+ "trustServerCertificate=false;"

+ "loginTimeout=30;";

ResultSet resultSet = null;

try (Connection connection = DriverManager.getConnection(connectionUrl);

Statement statement = connection.createStatement();) {

// Create and execute a SELECT SQL statement.

String selectSql = "SELECT TOP 10 Title, FirstName, LastName from SalesLT.Customer";

resultSet = statement.executeQuery(selectSql);

// Print results from select statement

while (resultSet.next()) {

System.out.println(resultSet.getString(2) + " " + resultSet.getString(3));

}

}

catch (SQLException e) {

e.printStackTrace();

}

}

}

步骤 3:插入行

在此示例中,执行 INSERT 语句,传递参数,并检索自动生成的主键值。

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.Statement;

public class SQLDatabaseConnection {

// Connect to your database.

// Replace server name, username, and password with your credentials

public static void main(String[] args) {

String connectionUrl =

"jdbc:sqlserver://yourserver.database.windows.net:1433;"

+ "database=AdventureWorks;"

+ "user=yourusername@yourserver;"

+ "password=;"

+ "encrypt=true;"

+ "trustServerCertificate=false;"

+ "loginTimeout=30;";

String insertSql = "INSERT INTO SalesLT.Product (Name, ProductNumber, Color, StandardCost, ListPrice, SellStartDate) VALUES "

+ "('NewBike', 'BikeNew', 'Blue', 50, 120, '2016-01-01');";

ResultSet resultSet = null;

try (Connection connection = DriverManager.getConnection(connectionUrl);

PreparedStatement prepsInsertProduct = connection.prepareStatement(insertSql, Statement.RETURN_GENERATED_KEYS);) {

prepsInsertProduct.execute();

// Retrieve the generated key from the insert.

resultSet = prepsInsertProduct.getGeneratedKeys();

// Print the ID of the inserted row.

while (resultSet.next()) {

System.out.println("Generated: " + resultSet.getString(1));

}

}

// Handle any errors that may have occurred.

catch (Exception e) {

e.printStackTrace();

}

}

}

另请参阅

示例 JDBC 驱动程序应用程序


相关推荐

中国电信客服电话人工服务号码是多少
诡店小掌柜
【原】《魔兽世界》魔兽绷带获取与使用技巧
15种常见香料的特性及用途,全都是干货知识(附万能卤水配方)
手机温度监测软件推荐:哪款最适合你?
苹果6s怎么升级系统