0% found this document useful (0 votes)
2 views3 pages

DB 1 Program

This Java program connects to an embedded Derby database named 'universitybd' and retrieves data from the 'faculty' table. It prints the ID, age, name, department, and salary of each faculty member to the console. The program includes error handling for SQL and class loading exceptions.

Uploaded by

mkpyy74hc6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

DB 1 Program

This Java program connects to an embedded Derby database named 'universitybd' and retrieves data from the 'faculty' table. It prints the ID, age, name, department, and salary of each faculty member to the console. The program includes error handling for SQL and class loading exceptions.

Uploaded by

mkpyy74hc6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.sql.

* ; // for standard JDBC programs:

import java.math.* ; // for BigDecimal and BigInteger support

public class JavaDataBase

static String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";

static String URL = " jdbc:derby:universitybd;create=true";

static String USER = "dleiba";

static String PASS = "855800452";

public static void main( String [] args)

Connection conn;

Statement stmt;

try

Class.forName(DRIVER);

conn = DriverManager.getConnection(URL, USER, PASS);

Statement stmt =conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,


ResultSet.CONCUR_READ_ONLY);

String sql;

sql = "SELECT * FROM faculty";

ResultSet rs = stmt.executeQuery(sql);

while(rs.next( ))

{
//Retrieve by column name

int id = rs.getInt("id");

int age = rs.getInt("age");

String name = rs.getString("name");

String department = rs.getString("dept");

double salary = rs.getDouble( "salary" );

//Display values

System.out.print("ID: " + id);

System.out.print(", Age: " + age);

System.out.print(", Name: " + name);

System.out.print(", DEPARTMENT: " + department);

System.out.println(", SALARY " + salary);

rs.close( );

stmt.close( );

conn.close( );

catch(SQLException se)

//Handle errors for JDBC

se.printStackTrace( );

catch(Exception e)

//Handle errors for Class.forName

e.printStackTrace( );

}
System.out.println("Goodbye!");

You might also like