• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

program to make start-connect by (oracle )

 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic