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

Conn DB

This document outlines the steps to connect and access a database with Java: 1. Build the database 2. Create an ODBC data source by configuring the Data Sources tool to connect to the Microsoft Access database 3. Write Java code using JDBC to execute SQL statements like SELECT, UPDATE, and INSERT on the database

Uploaded by

Soumia Blh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Conn DB

This document outlines the steps to connect and access a database with Java: 1. Build the database 2. Create an ODBC data source by configuring the Data Sources tool to connect to the Microsoft Access database 3. Write Java code using JDBC to execute SQL statements like SELECT, UPDATE, and INSERT on the database

Uploaded by

Soumia Blh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

steps of connection database access whit java:

1_build database:

2_making odbc for database access:


**go to control panel>>administrative tools>> Data Sources
(ODBC)>>System Dsn>>click button ADD>>choose Microsoft access
driver>>choose select in database panel>>choose your database which you
built>>set any name for data source name>>press ok.

example select statement:


import java.sql.*;

class Test_select {
public static void main(String[] args) {
Connection c = null;
Statement s = null;
ResultSet r = null;

try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c = DriverManager.getConnection("jdbc:odbc:db_test","","");
s = c.createStatement();

r = s.executeQuery("select * from test");


while(r.next())
{
System.out.println("name is "+ r.getString("name") +"and age is "+r.getInt("age"));
}}
catch(Exception e1){
System.out.println(e1);}

} }
example update statement:

Connection c = null;
Statement s = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c = DriverManager.getConnection("jdbc:odbc:db_test","","");

PreparedStatement stat = c.prepareStatement("UPDATE


test SET name=’mazen’ WHERE st_id=3");
stat.execute();
} catch (Exception ex) {
System.out.println("Error adding " + ex.toString());
}

example insert statement:


Connection c = null;
Statement s = null;

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c = DriverManager.getConnection("jdbc:odbc:db_test","","");

PreparedStatement stat = c.prepareStatement("insert


into test(name,age) values(?,?)");
stat.setString(1,’khaled’);
stat.setString(2,’smeer’);
stat.execute();
} catch (Exception ex) {
System.out.println("Error adding " + ex.toString());
}
All that done by omar abdo

You might also like