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

Connection File

This Java program establishes a connection to a MySQL database using JDBC. It loads the MySQL driver and connects to a database named 'hotelman' with specified credentials. The connection object is returned for further use, and exceptions are handled appropriately.

Uploaded by

Semmy Punny
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)
5 views2 pages

Connection File

This Java program establishes a connection to a MySQL database using JDBC. It loads the MySQL driver and connects to a database named 'hotelman' with specified credentials. The connection object is returned for further use, and exceptions are handled appropriately.

Uploaded by

Semmy Punny
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

// Java Program to Insert Details in a Table using JDBC

// Connections class

// Importing all SQL classes

import [Link].*;

public class connection {

// object of Connection class

// initially assigned NULL

Connection con = null;

public static Connection connectDB()

try {

// Step 2 is involved among 7 in Connection

// class i.e Load and register drivers

// 2(a) Loading drivers using forName() method

// name of database here is mysql

[Link]("[Link]");

// 2(b) Registering drivers using DriverManager

Connection con = [Link](

"jdbc:mysql://localhost:3306/hotelman",

"root", "1234");

// For DB here (custom sets)

// root is the username, and


// 1234 is the password

// returning the object of Connection class

// to be used in main class (Example2)

return con;

// Catch block to handle the exceptions

catch (SQLException | ClassNotFoundException e) {

// Print the exceptions

[Link](e);

return null;

}
}

You might also like