SlideShare a Scribd company logo
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();
}
}
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");
// 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() )
{
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");
// 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();
}
}

More Related Content

PDF
Struts database access
KEY
JDBC Basics (In 20 Minutes Flat)
ODP
Unit testing: unitils & dbmaintain
PDF
Lecture17
PPTX
Java database connectivity
PPTX
Jdbc ppt
PPT
Java database connectivity with MYSQL
Struts database access
JDBC Basics (In 20 Minutes Flat)
Unit testing: unitils & dbmaintain
Lecture17
Java database connectivity
Jdbc ppt
Java database connectivity with MYSQL

What's hot (17)

PPTX
Jdbc
PPTX
java script
PDF
React Native: JS MVC Meetup #15
ODP
DbSetup
PDF
Maven + Jsf + Richfaces + Jxl + Jdbc - Complete Code Example
ODT
Mysql
PDF
Windows 7 64 java envirenment build
PDF
Jdbc[1]
PDF
Continuous Integration and Drupal
PPTX
Mapping and listing in mule
PDF
OpenDMS - the first 2 weeks
PDF
#31.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
PPTX
Mapping and listing with mule
ODP
Running ms sql stored procedures in mule
ODP
Assertj-DB
PPTX
Mapping and listing with mule
Jdbc
java script
React Native: JS MVC Meetup #15
DbSetup
Maven + Jsf + Richfaces + Jxl + Jdbc - Complete Code Example
Mysql
Windows 7 64 java envirenment build
Jdbc[1]
Continuous Integration and Drupal
Mapping and listing in mule
OpenDMS - the first 2 weeks
#31.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
Mapping and listing with mule
Running ms sql stored procedures in mule
Assertj-DB
Mapping and listing with mule
Ad

Viewers also liked (12)

PDF
Trendsilvania: The Game
PPT
Body of Work2
PDF
PDF
Catherine Calanni Resume 3 5 15
PDF
Data mining chapter04and5-best
PDF
Java how to_program__7th_edition
PPT
Chapter 2 multimedia authoring and tools
PPT
Chapter 1 introduction to multimedia
PDF
Cloud Computing Impact on SE by Arman Taherian
PDF
Concorso shoah
PPTX
Livescores
PDF
Tayyab ul warda fi sharha burda by allama abul hasnat syed muhammad ahmad qadri
Trendsilvania: The Game
Body of Work2
Catherine Calanni Resume 3 5 15
Data mining chapter04and5-best
Java how to_program__7th_edition
Chapter 2 multimedia authoring and tools
Chapter 1 introduction to multimedia
Cloud Computing Impact on SE by Arman Taherian
Concorso shoah
Livescores
Tayyab ul warda fi sharha burda by allama abul hasnat syed muhammad ahmad qadri
Ad

Similar to Db examples (20)

PDF
Jdbc tutorial
PPT
Jdbc
PPT
JDBC Tutorial
PDF
22jdbc
PDF
Tomcat连接池配置方法V2.1
PPT
Data Access with JDBC
PPT
JDBC Connecticity.ppt
PPTX
Database connect
PDF
JDBC programming
DOCX
Scrollable Test App
PPTX
Lecture 1. java database connectivity
DOC
Java database connectivity notes for undergraduate
PPTX
Amr Mohamed Abd Elhamid_JAVA_JDBCData.pptx
DOCX
This is a basic JAVA pgm that contains all of the major compoents of DB2
PPT
30 5 Database Jdbc
PPT
4. Database Connectivity using JDBC .ppt
PPT
JDBC for CSQL Database
PPT
PPT
Refactoring Jdbc Programming
PPT
Web based development
Jdbc tutorial
Jdbc
JDBC Tutorial
22jdbc
Tomcat连接池配置方法V2.1
Data Access with JDBC
JDBC Connecticity.ppt
Database connect
JDBC programming
Scrollable Test App
Lecture 1. java database connectivity
Java database connectivity notes for undergraduate
Amr Mohamed Abd Elhamid_JAVA_JDBCData.pptx
This is a basic JAVA pgm that contains all of the major compoents of DB2
30 5 Database Jdbc
4. Database Connectivity using JDBC .ppt
JDBC for CSQL Database
Refactoring Jdbc Programming
Web based development

Recently uploaded (20)

PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PPT
Quality review (1)_presentation of this 21
PPTX
Introduction to Knowledge Engineering Part 1
PDF
Business Analytics and business intelligence.pdf
PPTX
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
PPTX
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
PPTX
oil_refinery_comprehensive_20250804084928 (1).pptx
PDF
annual-report-2024-2025 original latest.
PPTX
1_Introduction to advance data techniques.pptx
PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
PDF
.pdf is not working space design for the following data for the following dat...
PPTX
Qualitative Qantitative and Mixed Methods.pptx
PPTX
Introduction to machine learning and Linear Models
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPTX
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
PDF
Foundation of Data Science unit number two notes
PDF
Mega Projects Data Mega Projects Data
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
Quality review (1)_presentation of this 21
Introduction to Knowledge Engineering Part 1
Business Analytics and business intelligence.pdf
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
oil_refinery_comprehensive_20250804084928 (1).pptx
annual-report-2024-2025 original latest.
1_Introduction to advance data techniques.pptx
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
.pdf is not working space design for the following data for the following dat...
Qualitative Qantitative and Mixed Methods.pptx
Introduction to machine learning and Linear Models
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
Business Ppt On Nestle.pptx huunnnhhgfvu
Acceptance and paychological effects of mandatory extra coach I classes.pptx
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
Foundation of Data Science unit number two notes
Mega Projects Data Mega Projects Data

Db examples

  • 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(); } }