1
0
mirror of https://github.com/Microsoft/sql-server-samples.git synced 2025-12-08 14:58:54 +00:00
Files
sql-server-samples/samples/connect/java/connect.java
Philippe Marschall 951bae3040 Update Java samples
The Java samples have quite a lot of room for improvement.

I changed the following:

 - make the code compile
 - update the readme to point to the latest driver
 - remove one leading tab from every line
 - remove redundant imports
 - use Java 7 try-with-resources
 - release database objects as early as possible
 - propagate exceptions
 - use bind parameters to avoid SQL-injection vulnerabilities when the
   code is copy and pasted and adjusted
  - I had to guess on some of the parameter types since I didn't have a
    schema. I had a look at ql_in-memory_oltp_sample.sql. Executable
    samples with a continuous integration build would be a nice future
    improvement.
2016-08-07 12:08:30 +02:00

61 lines
2.0 KiB
Java

// Use the JDBC driver
import java.math.BigDecimal;
import java.sql.*;
import java.time.LocalDate;
public class connect {
// Connect to your database.
// Replace server name, username, and password with your credentials
public static void main(String[] args) throws SQLException {
String connectionString =
"jdbc:<your_servername>;"
+ "database=<your_databasename>;"
+ "user=<your_username@your_servername>;"
+ "password=<your_password>;"
+ "encrypt=true;"
+ "trustServerCertificate=false;"
+ "loginTimeout=30;";
try (Connection connection = DriverManager.getConnection(connectionString)) {
// Create and execute a SELECT SQL statement.
String selectSql = "SELECT TOP 10 Title, FirstName, LastName from SalesLT.Customer";
try (Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(selectSql)) {
// Print results from select statement
while (resultSet.next()) {
System.out.println(resultSet.getString(2) + " "
+ resultSet.getString(3));
}
}
// Create and execute an INSERT SQL prepared statement.
String insertSql = "INSERT INTO SalesLT.Product (Name, ProductNumber, Color, StandardCost, ListPrice, SellStartDate) VALUES "
+ "(?, ?, ?, ?, ?, ?);";
try (PreparedStatement prepsInsertProduct = connection.prepareStatement(
insertSql,
Statement.RETURN_GENERATED_KEYS)) {
prepsInsertProduct.setString(1, "Bike");
prepsInsertProduct.setString(2, "B1");
prepsInsertProduct.setString(3, "Blue");
prepsInsertProduct.setBigDecimal(4, new BigDecimal(50));
prepsInsertProduct.setBigDecimal(5, new BigDecimal(120));
prepsInsertProduct.setObject(6, LocalDate.of(2016, 1, 1).atStartOfDay());
prepsInsertProduct.executeUpdate();
// Retrieve the generated key from the insert.
try (ResultSet generatedKeys = prepsInsertProduct.getGeneratedKeys()) {
// Print the ID of the inserted row.
while (generatedKeys.next()) {
System.out.println("Generated: " + generatedKeys.getString(1));
}
}
}
}
}
}