SlideShare une entreprise Scribd logo
1  sur  4
1. Set: ODBC driver for JDBC connections
2. Connects: to Access database file named
3. Import the classes to connect to the database
4. Create a Statement object to execute the SQL query
5. Cleanup after finishing the job
import java.sql.*;
public class TestDB {
public static void main(String[] args) {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String database="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=dbCustomer.mdb;";
Connection conn=DriverManager.getConnection(database,"","");
Statement s=conn.createStatement();
for(int i=1;i<=10;i++){
String addRow="INSERT INTO tbCustomer Values(" + i + ",'Dara','M','SiemReap','1/2/1920','012 33 44 55');";
s.execute(addRow);}
String deleteRow="Delete From tbCustomer where CustID=1;";
s.execute(deleteRow);
String updateRow="Update tbCustomer Set CustName='Lola', Gender='F',Address='Siem Reap',
DOB='1/2/1920',PhoneNo='010220033' Where CustID=2;" ;
s.execute(updateRow);
String selTable="SELECT* FROM tbCustomer;";
s.execute(selTable);
s.close();
}
catch(Exception ex) {
ex.printStackTrace();
}
}}
Chapter vii(accessing databases with jdbc)

Contenu connexe

Tendances

My sql with java
My sql with javaMy sql with java
My sql with java
oly07104
 
Creating, Updating and Deleting Document in MongoDB
Creating, Updating and Deleting Document in MongoDBCreating, Updating and Deleting Document in MongoDB
Creating, Updating and Deleting Document in MongoDB
Wildan Maulana
 
Connection Database
Connection DatabaseConnection Database
Connection Database
jdkkamal
 
บทที่3
บทที่3บทที่3
บทที่3
Palm Unnop
 

Tendances (20)

Jdbc
JdbcJdbc
Jdbc
 
Connecting and using PostgreSQL database with psycopg2 [Python 2.7]
Connecting and using PostgreSQL database with psycopg2 [Python 2.7]Connecting and using PostgreSQL database with psycopg2 [Python 2.7]
Connecting and using PostgreSQL database with psycopg2 [Python 2.7]
 
Client Server Communication on iOS
Client Server Communication on iOSClient Server Communication on iOS
Client Server Communication on iOS
 
My sql with java
My sql with javaMy sql with java
My sql with java
 
The Ring programming language version 1.5.3 book - Part 28 of 184
The Ring programming language version 1.5.3 book - Part 28 of 184The Ring programming language version 1.5.3 book - Part 28 of 184
The Ring programming language version 1.5.3 book - Part 28 of 184
 
3 database-jdbc(1)
3 database-jdbc(1)3 database-jdbc(1)
3 database-jdbc(1)
 
JDBC
JDBCJDBC
JDBC
 
Java database connectivity with MYSQL
Java database connectivity with MYSQLJava database connectivity with MYSQL
Java database connectivity with MYSQL
 
JDBC Basics (In 20 Minutes Flat)
JDBC Basics (In 20 Minutes Flat)JDBC Basics (In 20 Minutes Flat)
JDBC Basics (In 20 Minutes Flat)
 
Creating, Updating and Deleting Document in MongoDB
Creating, Updating and Deleting Document in MongoDBCreating, Updating and Deleting Document in MongoDB
Creating, Updating and Deleting Document in MongoDB
 
Connection Database
Connection DatabaseConnection Database
Connection Database
 
Jdbc
JdbcJdbc
Jdbc
 
Arango DB for rubyists in 10mins
Arango DB for rubyists in 10minsArango DB for rubyists in 10mins
Arango DB for rubyists in 10mins
 
บทที่3
บทที่3บทที่3
บทที่3
 
Lokijs
LokijsLokijs
Lokijs
 
Overview on NoSQL and MongoDB
Overview on NoSQL and MongoDBOverview on NoSQL and MongoDB
Overview on NoSQL and MongoDB
 
MySQL Without the SQL - Oh My! August 2nd presentation at Mid Atlantic Develo...
MySQL Without the SQL - Oh My! August 2nd presentation at Mid Atlantic Develo...MySQL Without the SQL - Oh My! August 2nd presentation at Mid Atlantic Develo...
MySQL Without the SQL - Oh My! August 2nd presentation at Mid Atlantic Develo...
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
Datacon LA - MySQL without the SQL - Oh my!
Datacon LA - MySQL without the SQL - Oh my! Datacon LA - MySQL without the SQL - Oh my!
Datacon LA - MySQL without the SQL - Oh my!
 
JDBC ppt
JDBC pptJDBC ppt
JDBC ppt
 

En vedette

Analisis de sistema
Analisis de sistema Analisis de sistema
Analisis de sistema
wilfredo1993
 
TUSHAR APTE LATEST RESUME (2)
TUSHAR APTE LATEST  RESUME (2)TUSHAR APTE LATEST  RESUME (2)
TUSHAR APTE LATEST RESUME (2)
tushar apte
 
Confundimos el medio con el fin
Confundimos el medio con el finConfundimos el medio con el fin
Confundimos el medio con el fin
Jose Mora
 
Presentation of aviation
Presentation of aviationPresentation of aviation
Presentation of aviation
Archana Negi
 
Native american cultures
Native american culturesNative american cultures
Native american cultures
madelinejweiss
 
Guion Noticiero Reconversion
Guion Noticiero ReconversionGuion Noticiero Reconversion
Guion Noticiero Reconversion
Sagrario Orellana
 
M-U-H-A-M-M-A-D-N-A-H-E-E-M
M-U-H-A-M-M-A-D-N-A-H-E-E-MM-U-H-A-M-M-A-D-N-A-H-E-E-M
M-U-H-A-M-M-A-D-N-A-H-E-E-M
Muhammad Naeem
 

En vedette (18)

Winter DDS & Cut Plus - Esite
Winter DDS & Cut Plus - EsiteWinter DDS & Cut Plus - Esite
Winter DDS & Cut Plus - Esite
 
Analisis de sistema
Analisis de sistema Analisis de sistema
Analisis de sistema
 
Feliz día del padre
Feliz día del padreFeliz día del padre
Feliz día del padre
 
Campaña educativa
Campaña educativaCampaña educativa
Campaña educativa
 
Que es el inventario
Que es el inventarioQue es el inventario
Que es el inventario
 
Control números complejos
Control números complejosControl números complejos
Control números complejos
 
TUSHAR APTE LATEST RESUME (2)
TUSHAR APTE LATEST  RESUME (2)TUSHAR APTE LATEST  RESUME (2)
TUSHAR APTE LATEST RESUME (2)
 
Polígonos Regulares
Polígonos RegularesPolígonos Regulares
Polígonos Regulares
 
Prior Concussion Injuries Influence Recovery Time in College Athletes
Prior Concussion Injuries Influence Recovery Time in College AthletesPrior Concussion Injuries Influence Recovery Time in College Athletes
Prior Concussion Injuries Influence Recovery Time in College Athletes
 
Confundimos el medio con el fin
Confundimos el medio con el finConfundimos el medio con el fin
Confundimos el medio con el fin
 
Presentation of aviation
Presentation of aviationPresentation of aviation
Presentation of aviation
 
Mercado de altura, América Economía Internacional junio 2013
Mercado de altura, América Economía Internacional junio 2013Mercado de altura, América Economía Internacional junio 2013
Mercado de altura, América Economía Internacional junio 2013
 
Native american cultures
Native american culturesNative american cultures
Native american cultures
 
ความยิ่งใหญ่ของสถานที่ไกลออกไปบนท้องฟ้า. Thai (ภาษาไทย)
ความยิ่งใหญ่ของสถานที่ไกลออกไปบนท้องฟ้า. Thai (ภาษาไทย)ความยิ่งใหญ่ของสถานที่ไกลออกไปบนท้องฟ้า. Thai (ภาษาไทย)
ความยิ่งใหญ่ของสถานที่ไกลออกไปบนท้องฟ้า. Thai (ภาษาไทย)
 
Arroz con leche
Arroz con lecheArroz con leche
Arroz con leche
 
Petras
PetrasPetras
Petras
 
Guion Noticiero Reconversion
Guion Noticiero ReconversionGuion Noticiero Reconversion
Guion Noticiero Reconversion
 
M-U-H-A-M-M-A-D-N-A-H-E-E-M
M-U-H-A-M-M-A-D-N-A-H-E-E-MM-U-H-A-M-M-A-D-N-A-H-E-E-M
M-U-H-A-M-M-A-D-N-A-H-E-E-M
 

Similaire à Chapter vii(accessing databases with jdbc)

jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.ppt
DrMeenakshiS
 

Similaire à Chapter vii(accessing databases with jdbc) (20)

Lecture17
Lecture17Lecture17
Lecture17
 
Jdbc sasidhar
Jdbc  sasidharJdbc  sasidhar
Jdbc sasidhar
 
java arlow jdbc tutorial(java programming tutorials)
java arlow jdbc tutorial(java programming tutorials)java arlow jdbc tutorial(java programming tutorials)
java arlow jdbc tutorial(java programming tutorials)
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
Jdbc
JdbcJdbc
Jdbc
 
Java Web Programming Using Cloud Platform: Module 3
Java Web Programming Using Cloud Platform: Module 3Java Web Programming Using Cloud Platform: Module 3
Java Web Programming Using Cloud Platform: Module 3
 
Java Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet AdvancedJava Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet Advanced
 
22jdbc
22jdbc22jdbc
22jdbc
 
Jdbc day-1
Jdbc day-1Jdbc day-1
Jdbc day-1
 
jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.ppt
 
Jdbc drivers
Jdbc driversJdbc drivers
Jdbc drivers
 
Slide Latihan JDBC
Slide Latihan JDBCSlide Latihan JDBC
Slide Latihan JDBC
 
Jdbc
JdbcJdbc
Jdbc
 
10 J D B C
10  J D B C10  J D B C
10 J D B C
 
Mule esb – connecting to ms sql db
Mule esb – connecting to ms sql dbMule esb – connecting to ms sql db
Mule esb – connecting to ms sql db
 
Jsp project module
Jsp project moduleJsp project module
Jsp project module
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc connectivity
Jdbc connectivityJdbc connectivity
Jdbc connectivity
 
Final Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.pptFinal Database Connectivity in JAVA.ppt
Final Database Connectivity in JAVA.ppt
 

Plus de Chhom Karath (20)

set1.pdf
set1.pdfset1.pdf
set1.pdf
 
Set1.pptx
Set1.pptxSet1.pptx
Set1.pptx
 
orthodontic patient education.pdf
orthodontic patient education.pdforthodontic patient education.pdf
orthodontic patient education.pdf
 
New ton 3.pdf
New ton 3.pdfNew ton 3.pdf
New ton 3.pdf
 
ច្បាប់ញូតុនទី៣.pptx
ច្បាប់ញូតុនទី៣.pptxច្បាប់ញូតុនទី៣.pptx
ច្បាប់ញូតុនទី៣.pptx
 
Control tipping.pptx
Control tipping.pptxControl tipping.pptx
Control tipping.pptx
 
Bulbous loop.pptx
Bulbous loop.pptxBulbous loop.pptx
Bulbous loop.pptx
 
brush teeth.pptx
brush teeth.pptxbrush teeth.pptx
brush teeth.pptx
 
bracket size.pptx
bracket size.pptxbracket size.pptx
bracket size.pptx
 
arch form KORI copy.pptx
arch form KORI copy.pptxarch form KORI copy.pptx
arch form KORI copy.pptx
 
Bracket size
Bracket sizeBracket size
Bracket size
 
Couple
CoupleCouple
Couple
 
ច្បាប់ញូតុនទី៣
ច្បាប់ញូតុនទី៣ច្បាប់ញូតុនទី៣
ច្បាប់ញូតុនទី៣
 
Game1
Game1Game1
Game1
 
Shoe horn loop
Shoe horn loopShoe horn loop
Shoe horn loop
 
Opus loop
Opus loopOpus loop
Opus loop
 
V bend
V bendV bend
V bend
 
Closing loop
Closing loopClosing loop
Closing loop
 
Maxillary arch form
Maxillary arch formMaxillary arch form
Maxillary arch form
 
Front face analysis
Front face analysisFront face analysis
Front face analysis
 

Dernier

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
SoniaTolstoy
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Dernier (20)

Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 

Chapter vii(accessing databases with jdbc)

  • 1.
  • 2. 1. Set: ODBC driver for JDBC connections 2. Connects: to Access database file named 3. Import the classes to connect to the database 4. Create a Statement object to execute the SQL query 5. Cleanup after finishing the job
  • 3. import java.sql.*; public class TestDB { public static void main(String[] args) { try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String database="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=dbCustomer.mdb;"; Connection conn=DriverManager.getConnection(database,"",""); Statement s=conn.createStatement(); for(int i=1;i<=10;i++){ String addRow="INSERT INTO tbCustomer Values(" + i + ",'Dara','M','SiemReap','1/2/1920','012 33 44 55');"; s.execute(addRow);} String deleteRow="Delete From tbCustomer where CustID=1;"; s.execute(deleteRow); String updateRow="Update tbCustomer Set CustName='Lola', Gender='F',Address='Siem Reap', DOB='1/2/1920',PhoneNo='010220033' Where CustID=2;" ; s.execute(updateRow); String selTable="SELECT* FROM tbCustomer;"; s.execute(selTable); s.close(); } catch(Exception ex) { ex.printStackTrace(); } }}

Notes de l'éditeur

  1. /** * File: databaseCon.java * Tiltle: Database Connection Using MS Access * Author: http://java-program-sample.blogspot.com/ */ //Java Core Package import javax.swing.*; //Java Extension Package import java.awt.*; import java.awt.event.*; import java.sql.*; public class TestConnect1 extends JFrame implements ActionListener { //Initializing components private JButton connect; private JTextField confirmation; Connection con; Statement st; ResultSet rs; String db; //Setting up GUI public TestConnect1() { //Setting up the Title of the Window super("MS Access Connection"); //Set Size of the Window (WIDTH, HEIGHT) setSize(250,95); //Exit Property of the Window setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Constructing Components connect = new JButton("Test Connection"); confirmation = new JTextField(20); //Setting up the container ready for the components to be added. Container pane = getContentPane(); setContentPane(pane); //Setting up the container layout FlowLayout flow = new FlowLayout(FlowLayout.CENTER); pane.setLayout(flow); //Creating a connection to MS Access and fetching errors using "try-catch" to check if it is successfully connected or not. try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); db = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=database.mdb;"; con = DriverManager.getConnection(db,"",""); st = con.createStatement(); confirmation.setText("Successfully Connected to Database"); } catch (Exception e) { JOptionPane.showMessageDialog(null,"Failed to Connect to Database","Error Connection", JOptionPane.WARNING_MESSAGE); System.exit(0); } //Adding Event Listener to the button "Connect" connect.addActionListener(this); //Adding components to the container pane.add(confirmation); pane.add(connect); /**Set all the Components Visible. * If it is set to "false", the components in the container will not be visible. */ setVisible(true); } //Creating an event to the JButton "Connect" public void actionPerformed(ActionEvent event) { try { if(event.getSource() == connect ) { //Adding values on the database field "confirm" and "confirm_to" String insert = "insert into Confirm (confirm, confirm_to) values ('"+confirmation.getText()+"','"+confirmation.getText()+"')"; st.execute(insert); //Execute the sql //This will display if the connection and the insertion of data to the database is successful. confirmation.setText("Test Successful"); //Display what is in the database rs=st.executeQuery("select * from Confirm"); while(rs.next()) { System.out.println(rs.getString("confirm")); } } }catch(Exception e) { JOptionPane.showMessageDialog(null,"Failed to Connect to Database","Error Connection", JOptionPane.WARNING_MESSAGE); System.exit(0); } } //Main Method public static void main (String[] args) { TestConnect1 pjtf = new TestConnect1(); } }