0% found this document useful (0 votes)
10 views1 page

PracticalNo18 2

This document outlines a practical exercise involving JDBC to insert and retrieve data from a database. It includes a Java program that connects to a MySQL database, creates a table named 'Employee', and handles potential SQL exceptions. The student's name is Mavani Ashish Shantilal, and the roll number is 47.

Uploaded by

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

PracticalNo18 2

This document outlines a practical exercise involving JDBC to insert and retrieve data from a database. It includes a Java program that connects to a MySQL database, creates a table named 'Employee', and handles potential SQL exceptions. The student's name is Mavani Ashish Shantilal, and the roll number is 47.

Uploaded by

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

Practical No.

: 18

Practical Name: Write a program to insert and retrieve data from database using JDBC.

Roll No.: 47

Student Name: Mavani Ashish Shantilal

Program:
import java.sql.*;

public class practical18_2


{
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/employee_DB";
String user = "root";
String password = "";

try (Connection conn = DriverManager.getConnection(url, user, password);


Statement stmt = conn.createStatement()) {

String createTableSQL = "CREATE TABLE IF NOT EXISTS Employee (" +


"emp_id INT PRIMARY KEY AUTO_INCREMENT," +
"emp_name VARCHAR(50))";
stmt.executeUpdate(createTableSQL);

System.out.println("Table created successfully.");


} catch (SQLException e) {
e.printStackTrace();
System.out.println("Error: " + e.getMessage());
}
}
}

Output:

You might also like