Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

program to make start-connect by (oracle )

 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am only simply making the program to have output like given in this page
I have coded in core-java with database in MS-Access having "testing" as System DSN.

In database table "relation" having only two column viz "parent" and "child"

PLEASE HELP ME TO GET FIRST OUTPUT GIVEN IN THE ABOVE LINK.

I tried it but getting error
D:\java_prac>javac db/DBConnection.java
db/DBConnection.java:43: rs1 is already defined in child(int)
ResultSet rs1= stmt1.executeQuery("SELECT * from relation where
parent=0");
^
db/DBConnection.java:45: rs1 is already defined in child(int)
ResultSet rs1= stmt1.executeQuery("SELECT * from relation where
parent="+parent);
^
2 errors

D:\java_prac>




following is the java code tried

-------------------

package db;

import java.util.*;
import java.sql.*;

public class DBConnection {

public Connection getConnection()
{
Connection con =null;

String dbDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String url= "jdbc dbc:testing";
try
{
Class.forName(dbDriver);
con = DriverManager.getConnection(url);
}
catch(Exception e)
{
System.out.println("Exception caught "+e);
e.printStackTrace();
}
return con;

}

public static void main(String args[]){

DBConnection dbcon= new DBConnection();
dbcon.child(0);
}


void child(int parent) {
int a=0;
//DBConnection dbcon1= new DBConnection();
Connection con1= this.getConnection();
Statement stmt1=con1.createStatement();
ResultSet rs1=null;

if(parent==0) {
ResultSet rs1= stmt1.executeQuery("SELECT * from relation where parent=0");
}else {
ResultSet rs1= stmt1.executeQuery("SELECT * from relation where parent="+parent);
}
if(rs1.next())
{
a=rs1.getInt(2);
System.out.println('\n' +" ");
while(rs1.next()) {
a=rs1.getInt(2);
System.out.println('\n' +" ");
DBConnection dbcon1= new DBConnection();
dbcon1.child(a);
}
}
else
return;
}
}

-----------

with reference to above link I put the entries shown as insert in above link page... but instead of null i putted "0"


HELP ME TO FIGURE IT OUT


REGARDS,
AMIT
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You only need to declare a variable once in a given method or class; after that you can refer to it without redeclaring it. To "declare" a variable, you give both its name and its type. This is a declaration:

ResultSet rs1 = null;

To refer to that variable again, you just use the name, and not the type:

rs1 = stmt1.executeQuery("SELECT * from relation where parent=0");
 
There is no "i" in denial. Tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic