Java SQL (JDBC) session, Summaries of Database Management Systems (DBMS)

Java SQL (JDBC) session. In this hands-on session, you will create a connection between Java and MySQL Server. Later, you will need to define several SQL ...

Typology: Summaries

2021/2022

Uploaded on 08/05/2022

nguyen_99
nguyen_99 🇻🇳

4.2

(80)

1K documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java$SQL$(JDBC)$session$
In# this# hands-on# session,# you# will# create# a# connection# between# Java# and# MySQL# Server.#
Later,# you# will# need# to# define# several# SQL# queries# to# create# a# database,# a# few# tables# and#
insert#corresponding#data#into#them.#
#
Exercise(instructions:(
1. Create#a#class#SQLdatabase#in#Eclipse.#
2. Register#JDBC#driver#and#open#a#connection#with#SQL#Server.#
3. Create#database#STUDENTS.#
4. Create#a#table#REGISTRATION#and#insert#the#following#values:#
ID(primary(key)#
First#name#
Last#name#
Age#
100#
Anders#
Berg#
21#
101#
Anna#
Bellini#
20#
102#
Steve#
Warlock#
22#
103#
Sumit#
Mittal#
24#
5. Update#age#of#Sumit(Mittal#to#23(years,#using#PreparedStatement.#
6. Insert#a#new#value#into#the#table#(104,#Todor,#Nicolescu,#27),#using#
PreparedStatement.#
#
#
Code#structure:#
import java.sql.*;
public class SQLdatabase {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/";
// Database credentials
static final String USER = "root";
static final String PASS = "root"; // insert the password to SQL server
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
// Register JDBC driver
Class.forName(JDBC_DRIVER);
// Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
/ // Execute a query to create database
System.out.println("Creating database...");
pf2

Partial preview of the text

Download Java SQL (JDBC) session and more Summaries Database Management Systems (DBMS) in PDF only on Docsity!

Java SQL (JDBC) session

In this hands-on session, you will create a connection between Java and MySQL Server.

Later, you will need to define several SQL queries to create a database, a few tables and

insert corresponding data into them.

Exercise instructions:

1. Create a class SQLdatabase in Eclipse.

2. Register JDBC driver and open a connection with SQL Server.

3. Create database STUDENTS.

4. Create a table REGISTRATION and insert the following values:

ID( primary key ) First name Last name Age

100 Anders Berg 21

101 Anna Bellini 20

102 Steve Warlock 22

103 Sumit Mittal 24

5. Update age of Sumit Mittal to 23 years , using PreparedStatement.

6. Insert a new value into the table (104, Todor, Nicolescu, 27), using

PreparedStatement.

Code structure:

import java.sql.*; public class SQLdatabase { // JDBC driver name and database URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/"; // Database credentials static final String USER = "root"; static final String PASS = "root"; // insert the password to SQL server public static void main(String[] args) { Connection conn = null ; Statement stmt = null ; try { // Register JDBC driver Class. forName ( JDBC_DRIVER ); // Open a connection System. out .println("Connecting to database..."); conn = DriverManager. getConnection ( DB_URL , USER , PASS ); / // Execute a query to create database System. out .println("Creating database...");

stmt = conn.createStatement(); String sql = "CREATE …"; // Create database students stmt.executeUpdate(sql); System. out .println("Database created successfully..."); // Connect to the created database STUDENTS and create table REGISTRATION conn = DriverManager. getConnection ( DB_URL + "STUDENTS", USER , PASS ); sql = "CREATE TABLE …”// Create table REGISTRATION with corresponding attributes stmt. ; System. out .println("Created table in given database successfully..."); // insert values into the table sql = "INSERT INTO Registration " + "VALUES (…)"; stmt. …; // repeat the procedure for all rows of the table System. out .println("Inserted records into the table..."); // create the java mysql update preparedstatement String query = "update registration …"; // Update age of Sumit Mittal PreparedStatement preparedStmt = conn.prepareStatement(query); … // execute p // insert a new values to the table with preparedstatement query = "insert into registration values(?, ?, ?, ?)"; // finish the statement . System. out .println("The table is updated..."); conn.close(); } catch (SQLException se){ //Handle errors for JDBC se.printStackTrace(); } catch (Exception e){ //Handle errors for Class.forName e.printStackTrace();} System. out .println("Goodbye!"); } }