0% found this document useful (0 votes)
18 views

Java Practical No 27

The document contains Java code for connecting to a MySQL database and performing operations such as creating a 'Student' table and inserting a record. It includes error handling for database connection issues and resource management. The code demonstrates the use of JDBC for database interactions in Java.

Uploaded by

dipya2121
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)
18 views

Java Practical No 27

The document contains Java code for connecting to a MySQL database and performing operations such as creating a 'Student' table and inserting a record. It includes error handling for database connection issues and resource management. The code demonstrates the use of JDBC for database interactions in Java.

Uploaded by

dipya2121
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/ 5

Practical No 27

Title code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MySQLDatabaseConnection {
private static final String DB_URL = "jdbc:mysql://localhost:3306/example";
private static final String USER = "root";
private static final String PASSWORD = "root123";
public static Connection getConnection() {
Connection connection = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(DB_URL, USER,
PASSWORD);
System.out.println("MySQL Database connection established
successfully!");
} catch (ClassNotFoundException e) {
System.err.println("MySQL JDBC Driver not found: " + e.getMessage());
} catch (SQLException e) {
System.err.println("MySQL Database connection error: " +
e.getMessage());
}
return connection;
}
public static void closeConnection(Connection connection) {
if (connection != null) {
try {
connection.close();
System.out.println("MySQL Database connection closed.");
} catch (SQLException e) {
System.err.println("Error closing MySQL database connection: " +
e.getMessage());
}
}
}
public static void main(String[] args) {
Connection conn = null;
try {
conn = getConnection();
} catch (Exception e) {
e.printStackTrace();
} finally {
closeConnection(conn);
}
}
}

O/P:
Practical Related Questions:
1. Write a Program to create a Student
Table in database and insert a record in a
Student table.
Ans:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class StudentDatabaseOperations {
private static final String DB_URL = "jdbc:mysql://localhost:3306/example";
private static final String USER = "root";
private static final String PASSWORD = "root123";
public static void main(String[] args) {
Connection connection = null;
Statement statement = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(DB_URL, USER,
PASSWORD);
statement = connection.createStatement();
String createTableQuery = "CREATE TABLE IF NOT EXISTS Student (" +
"StudentId INT PRIMARY KEY, " +
"FirstName VARCHAR(50), " +
"LastName VARCHAR(50), " +
"Age INT, " +
"Grade VARCHAR(10))";
String insertRecordQuery = "INSERT INTO Student " +
"(StudentId, FirstName, LastName, Age, Grade) " +
"VALUES (1, 'John', 'Doe', 20, 'A')";
statement.executeUpdate(createTableQuery);
System.out.println("Student table created successfully.");
statement.executeUpdate(insertRecordQuery);
System.out.println("Record inserted successfully.");
} catch (ClassNotFoundException e) {
System.err.println("MySQL JDBC Driver not found: " + e.getMessage());
} catch (SQLException e) {
System.err.println("Database error: " + e.getMessage());
} finally {
try {
if (statement != null) statement.close();
if (connection != null) connection.close();
} catch (SQLException e) {
System.err.println("Error closing resources: " + e.getMessage());
}
}
}
}

O/P:

You might also like