Introduction
In
this blog we will know how to display records from database using JDBC (Java
Database Connectivity) in the console window.
Here
we use Type-1 driver (JDBC-ODBC bridge)
Creation of dsn(database source name) for Oracle
Start-Control
panel- Administrative Tools- Data Sources (ODBC)-go to system DSN tab-click add
button-select a driver for which you want to set up a data source (for Oracle-
Oracle in XE)-select it and click finish-give any name in the data source name
textbox-then click ok button.
Note:
- Here Username=system, Password=pintu and DSN name=dsn1
Table Creation
Create
table employee (empno int,empname varchar(50),sal int)
Example:- To
select a record from a table
-
- import java.sql.*;
- public class select
- {
- public static void main(String args[]) throws Exception {
-
-
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
-
- Connection con = DriverManager.getConnection("jdbc:odbc:dsn1", "system", "pintu");
-
- System.out.println("Connected to database");
- Statement stmt = con.createStatement();
-
- ResultSet rs = stmt.executeQuery("select * from employee");
-
- while (rs.next())
- {
-
- int x = rs.getInt("empno");
-
- String y = rs.getString("empname");
-
- int z = rs.getInt("sal");
- System.out.println(x + " " + y + " " + z);
- }
-
- con.close();
- }
- }
Compile
Javac
select.java
Java
select