The document contains 4 Java classes that demonstrate connecting to a MySQL database and performing basic CRUD (create, read, update, delete) operations. The first class DBTwoTables queries data from two tables and prints the results. The second class Db1 queries all rows from a single table. The third class DBUpdate updates table rows. The fourth class DBDelete deletes a table row. All classes establish a database connection, create SQL statements, execute the statements, handle exceptions, and close the connection.
1. 1. package Examples;
import java.sql.*;
public class DBTwoTables {
public DBTwoTables()
{
// Load the JDBC driver
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded");
// Establish a connection
Connection connection = DriverManager.getConnection
("jdbc:mysql://localhost/test", "root", "root");
System.out.println("Database connected");
// Create a statement
Statement statement = connection.createStatement();
// Execute a statement
ResultSet resultSet = statement.executeQuery("select
e.empId,e.empName,c.comBranch from tblCompany1 c,tblEmployee e where c.comNo=e.comNo
and c.comNo='com1' ");
// Iterate through the result and print the student names
while(resultSet.next() )
{
System.out.println(resultSet.getString(1)+ "t" +
resultSet.getString(2)+ "t" + resultSet.getString(3));
}
// Close the connection
connection.close();
}
catch (ClassNotFoundException ex )
{
System.out.println(ex.getMessage());
}
catch (SQLException ex )
{
System.out.println(ex.getMessage());
//ex.printStackTrace();
}
2. }
public static void main(String args[]) throws SQLException
,ClassNotFoundException
{
DBTwoTables d1=new DBTwoTables();
}
}
2. package Examples;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Bk
*/
/*import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.SQLException;
import java.sql.ResultSet;
*/
import java.sql.*;
public class Db1 {
public static void main(String args[]) throws
SQLException,ClassNotFoundException {
// Load the JDBC driver
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded");
// Establish a connection
Connection connection = DriverManager.getConnection
("jdbc:mysql://localhost/dbsample", "root", "root");
System.out.println("Database connected");
3. // Create a statement
Statement statement = connection.createStatement();
// Execute a statement
ResultSet resultSet = statement.executeQuery("select * from tblemployee");
// Iterate through the result and print the student names
while(resultSet.next() ){
System.out.println(resultSet.getString(1)+ "t" +
resultSet.getString(2)+ "t" + resultSet.getString(3));
}
// Close the connection
connection.close();
}
}
3. package Examples;
import java.sql.*;
public class DBUpdate {
public DBUpdate()
{
// Load the JDBC driver
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded");
// Establish a connection
Connection connection = DriverManager.getConnection
("jdbc:mysql://localhost/test", "root", "root");
System.out.println("Database connected");
// Create a statement
Statement statement = connection.createStatement();
// Execute a statement
statement.executeUpdate("update tblCompany1 set comBranch='Bole' where
comBranch like'S%' ");
System.out.println("the update is successful");
// Iterate through the result and print the student names
/*while(resultSet.next() )
4. {
System.out.println(resultSet.getString(1)+ "t" +
resultSet.getString(2)+ "t" + resultSet.getString(3));
}
*/
// Close the connection
connection.close();
}
catch (ClassNotFoundException ex )
{
System.out.println(ex.getMessage());
}
catch (SQLException ex )
{
System.out.println(ex.getMessage());
//ex.printStackTrace();
}
}
public static void main(String args[]) throws SQLException
,ClassNotFoundException
{
DBUpdate d1=new DBUpdate();
}
}
4.package Examples;
import java.sql.*;
public class DBDelete {
public DBDelete()
{
// Load the JDBC driver
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded");
5. // Establish a connection
Connection connection = DriverManager.getConnection
("jdbc:mysql://localhost/test", "root", "root");
System.out.println("Database connected");
// Create a statement
Statement statement = connection.createStatement();
// Execute a statement
statement.executeUpdate("delete from tblEmployee where empName='dady'
");
System.out.println("the update is successful");
// Iterate through the result and print the student names
/*while(resultSet.next() )
{
System.out.println(resultSet.getString(1)+ "t" +
resultSet.getString(2)+ "t" + resultSet.getString(3));
}
*/
// Close the connection
connection.close();
}
catch (ClassNotFoundException ex )
{
System.out.println(ex.getMessage());
}
catch (SQLException ex )
{
System.out.println(ex.getMessage());
//ex.printStackTrace();
}
}
public static void main(String args[]) throws SQLException
,ClassNotFoundException
{
DBDelete d1=new DBDelete();
}
}