0% found this document useful (0 votes)
78 views9 pages

Lab 2

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

Lab 2

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

LAB 2 - JDBC

Create a database named EMPDB:

Enter the following command and select Execute (Or press F5)
CREATE DATABASE EMPDB

Database created successfully:

Create Table:

We use the following statement:


use EMPDB
create table Employees (
id int not null primary key
,first nvarchar (255)
,last nvarchar (255)
,age int not null
)

Note, in a window editor, if we write many commands, which statement we want to execute, we must
select that statement.

Connect SQL Server to java:

Download Microsoft JDBC driver

Download JDBC Driver and add sqljdbc42.jar to Java project using NetBeans by following steps

Right-click Libraries -> Select Add JAR/Folder…

Specify the folder containing the JDBC Driver and select the file sqljdbc42.jar -> select Open
SQL Server JDBC connection string:

Each database management system will have a different connection string. Following is the JDBC
connection string for Microsoft SQL Server

jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]

serverName is the server name or IP address of the machine where Microsoft SQL Server is installed

instanceName is the name of an instance to connect to serverName. If this parameter is not specified,
the default instance will be used

portNumber is the SQL Server application port, default is 1433


property=value specifies the database name, name and password to login to SQL Server

databaseName=EMPDB; user=sa; password=123

establish connection:
String dbURL =
"jdbc:sqlserver://localhost;databaseName=EMPDB;user=sa;password=123";
Connection conn = DriverManager.getConnection(dbURL);
if (conn != null) {
System.out.println("Connected");
}
In order for the above code to execute successfully, we have to do 2 things

The first thing: Set up SQL Server to allow login using SQL Server's account

Right-click SQL Server -> Select Properties


Select SQL Server and Windows Authentication mode

Restart SQL Server


The second: Activate and set a password for the account sa

Right-click sa -> Select Properties

Set password
Activate the sa account: select Enable in the Login section (this account is disabled by default)
Example program:
package jdbc;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ConnectionTest {
public static void main(String[] args) {
try {
String dbURL =
"jdbc:sqlserver://localhost;databaseName=EMPDB;user=sa;passwo
rd=123";
Connection conn = DriverManager.getConnection(dbURL);
if (conn != null) {
System.out.println("Connected");
DatabaseMetaData dm = (DatabaseMetaData) conn.getMetaData();
System.out.println("Driver name: " + dm.getDriverName());
System.out.println("Driver version: " + dm.getDriverVersion());
System.out.println("Product name: " +
dm.getDatabaseProductName());
System.out.println("Product version: " +
dm.getDatabaseProductVersion());
}
} catch (SQLException ex) {
System.err.println("Cannot connect database, " + ex);
}
}
}
You write a program that executes the following requirements:

1. Display data from a table

2. Add a row to the table

3. Delete a row in the table

4. Update one row data in the table

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy