0% found this document useful (0 votes)
3 views2 pages

SQL Server JDBC Result Set Example

Uploaded by

etest2272
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

SQL Server JDBC Result Set Example

Uploaded by

etest2272
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

/*=====================================================================

File: [Link]
Summary: This Microsoft JDBC Driver for SQL Server sample application
demonstrates how to use a result set to retrieve a set of
data from a SQL Server database.
---------------------------------------------------------------------
This file is part of the Microsoft JDBC Driver for SQL Server Code Samples.
Copyright (C) Microsoft Corporation. All rights reserved.

This source code is intended only as a supplement to Microsoft


Development Tools and/or on-line documentation. See these other
materials for detailed information regarding Microsoft code samples.

THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.
=====================================================================*/
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class RetrieveResultSet {

public static void main(String[] args) {

// Create a variable for the connection string.


String connectionUrl =
"jdbc:sqlserver://<server>:<port>;databaseName=AdventureWorks;user=<user>;password=
<password>";

try (Connection con = [Link](connectionUrl); Statement


stmt = [Link]();) {
createTable(stmt);
String SQL = "SELECT * FROM [Link];";
ResultSet rs = [Link](SQL);
displayRow("PRODUCTS", rs);
}
// Handle any errors that may have occurred.
catch (SQLException e) {
[Link]();
}
}

private static void displayRow(String title,


ResultSet rs) throws SQLException {
[Link](title);
while ([Link]()) {
[Link]([Link]("ProductNumber") + " : " +
[Link]("Name"));
}
}

private static void createTable(Statement stmt) throws SQLException {


[Link]("if exists (select * from [Link] where name =
'Product_JDBC_Sample')"
+ "drop table Product_JDBC_Sample");
String sql = "CREATE TABLE [Product_JDBC_Sample](" + "[ProductID] [int]
IDENTITY(1,1) NOT NULL,"
+ "[Name] [varchar](30) NOT NULL," + "[ProductNumber] [nvarchar]
(25) NOT NULL,"
+ "[MakeFlag] [bit] NOT NULL," + "[FinishedGoodsFlag] [bit] NOT
NULL," + "[Color] [nvarchar](15) NULL,"
+ "[SafetyStockLevel] [smallint] NOT NULL," + "[ReorderPoint]
[smallint] NOT NULL,"
+ "[StandardCost] [money] NOT NULL," + "[ListPrice] [money] NOT
NULL," + "[Size] [nvarchar](5) NULL,"
+ "[SizeUnitMeasureCode] [nchar](3) NULL," +
"[WeightUnitMeasureCode] [nchar](3) NULL,"
+ "[Weight] [decimal](8, 2) NULL," + "[DaysToManufacture] [int] NOT
NULL,"
+ "[ProductLine] [nchar](2) NULL," + "[Class] [nchar](2) NULL," +
"[Style] [nchar](2) NULL,"
+ "[ProductSubcategoryID] [int] NULL," + "[ProductModelID] [int]
NULL,"
+ "[SellStartDate] [datetime] NOT NULL," + "[SellEndDate]
[datetime] NULL,"
+ "[DiscontinuedDate] [datetime] NULL," + "[rowguid]
[uniqueidentifier] ROWGUIDCOL NOT NULL,"
+ "[ModifiedDate] [datetime] NOT NULL,)";

[Link](sql);

sql = "INSERT Product_JDBC_Sample VALUES ('Adjustable Time','AR-


5381','0','0',NULL,'1000','750','0.00','0.00',NULL,NULL,NULL,NULL,'0',NULL,NULL,NUL
L,NULL,NULL,'2008-04-30 00:00:00.000',NULL,NULL,'694215B7-08F7-4C0D-ACB1-
D734BA44C0C8','2014-02-08 10:01:36.827') ";
[Link](sql);

sql = "INSERT Product_JDBC_Sample VALUES ('ML Bottom Bracket','BB-


8107','0','0',NULL,'1000','750','0.00','0.00',NULL,NULL,NULL,NULL,'0',NULL,NULL,NUL
L,NULL,NULL,'2008-04-30 00:00:00.000',NULL,NULL,'694215B7-08F7-4C0D-ACB1-
D734BA44C0C8','2014-02-08 10:01:36.827') ";
[Link](sql);

sql = "INSERT Product_JDBC_Sample VALUES ('Mountain-500 Black, 44','BK-


M18B-
44','0','0',NULL,'1000','750','0.00','0.00',NULL,NULL,NULL,NULL,'0',NULL,NULL,NULL,
NULL,NULL,'2008-04-30 00:00:00.000',NULL,NULL,'694215B7-08F7-4C0D-ACB1-
D734BA44C0C8','2014-02-08 10:01:36.827') ";
[Link](sql);
}
}

You might also like