stu nicholls dot com | menu - Professional dropdown #3



JDBC Program


//Program to retrieve the contents from the database when you query it
//with a specific Name
//Database name: db1
//Table Name: staff
//Fields in the table: ID, Name, Age
//Note: Steps to create DSN is given in jdbcSteps.php in the same portal

import java.sql.*;

import java.lang.*;

import java.io.*;

class NameSearch

{

public static void main(String args[]){

int i=0;

try{

// loading the driver

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String dbUrl="jdbc:odbc:ABC";

// getting the connection

Connection con=DriverManager.getConnection(dbUrl,"","");

//get content from table, create an object of statement class

Statement stmt=con.createStatement();

DataInputStream in=new DataInputStream(System.in);

System.out.print("\n\nEnter the first letter of the First name :-->");

String s=in.readLine();

//get response

ResultSet rs=stmt.executeQuery("Select * from staff where Name like

'"+s+"%'");

while(rs.next()) {

System.out.println("\nName is: "+rs.getString("Name"));

System.out.println("Age is: "+rs.getInt("Age"));

i=i+1;

}

System.out.println("\nNumber of records found: "+i);

if(i==0)

System.out.println("\n !!!...NO Records Found...!!!");

}

catch(Exception e)

{

e.getMessage();

}

}

}

Our aim is to provide information to the knowledge seekers. 

comments powered by Disqus
click here
click here
Footer1