Results 1 to 3 of 3

Thread: Jdbc

  1. #1
    Dineshmtech is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Jdbc

    Hello, Here i have this following problem that i have fetch the data from the database which is stored in global variable, when i am accessing these variables by another inherited class i can't able to get the values. When i run the other class it shows only a default array value. Any help will be appreciated, this is my coding,

    import java.io.*;
    import java.sql.*;
    import java.util.*;
    public class dbsort
    {
    int p=9;
    public static final int em[]=new int[30];
    public static void main(String args[])
    {
    dbsort obj=new dbsort();
    try{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con=DriverManager.getConnection("jdbc:odbc:myodbc" ,"root","root");
    String str="select empid from detail";
    Statement st=con.createStatement();
    ResultSet rs=st.executeQuery(str);
    System.out.println("Give the array value of data base");
    Scanner in = new Scanner(System.in);
    int j=in.nextInt();
    int i=1;
    while(rs.next())
    {
    obj.em[i]=rs.getInt("empid");
    //System.out.println("Emp id is " +obj.em[i]);
    i++;
    }
    //for(int k=0;k<obj.em.length;k++){
    //System.out.println(obj.em[k]);}
    con.close();
    }catch(Exception e){System.out.println(e);}
    obj.xxx();
    }
    public int[] xxx()
    {
    dbsort obj1=new dbsort();
    final int k[]=obj1.em;
    for (int l=1;l<obj1.em.length;l++)
    System.out.println(k[l]);
    return k;
    }
    }
    import java.io.*;
    public class exam2 extends dbsort
    {
    public static void main(String args[])
    {
    dbsort ob=new dbsort();
    int x[]=ob.xxx();
    for(int i=0;i<33;i++)
    System.out.println(x[i]);
    }
    }// i can't get any values from existing above mentioned class via this class.

    Thanks in advance.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Jdbc

    Please use [code] tags [/code] when posting code, otherwise it is very difficult to follow the flow of the code.

    Java Code:
    import java.io.*;
    import java.sql.*;
    import java.util.*;
    public class dbsort
    {	
    	int p=9;
    	public static final int em[]=new int[30];
    	public static void main(String args[])
    	{
    	dbsort obj=new dbsort();
    	try{
    	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    	Connection con=DriverManager.getConnection("jdbc:odbc:myodbc","root","root");
    	String str="select empid from detail";
    	Statement st=con.createStatement();
    	ResultSet rs=st.executeQuery(str);
    	System.out.println("Give the array value of data base");
    	Scanner in = new Scanner(System.in);	
    	int j=in.nextInt();
    	int i=1;	
    	while(rs.next())
    	{		
    	obj.em[i]=rs.getInt("empid");
    	//System.out.println("Emp id is " +obj.em[i]);	
    	i++;	
    	}
    	//for(int k=0;k<obj.em.length;k++){
    	//System.out.println(obj.em[k]);}	
    	con.close();
    	}catch(Exception e){System.out.println(e);}
    	obj.xxx();	
    	}
    	public int[] xxx()
    	{
    	dbsort obj1=new dbsort();
    	 final int k[]=obj1.em;	
    	for (int l=1;l<obj1.em.length;l++)
    	System.out.println(k[l]);
    	return k;
    	}
    }
    import java.io.*;
    public class exam2 extends dbsort
    {
    	public static void main(String args[])
    	{	
    	dbsort ob=new dbsort();
    	int x[]=ob.xxx();
            for(int i=0;i<33;i++)
    	System.out.println(x[i]);
    	}
    }
    Even with the tags your code is not formatted so that it is easy to follow.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Jdbc

    OK.
    How are you running this?
    Which main() method is your entry point (as there are two of them)?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 0
    Last Post: 03-03-2012, 01:01 AM
  2. JDBC Problem - com.mysql.jdbc.Driver
    By icu222much in forum Advanced Java
    Replies: 5
    Last Post: 11-22-2011, 04:54 PM
  3. Jdbc
    By HariPrasad in forum JDBC
    Replies: 1
    Last Post: 07-30-2009, 05:07 AM
  4. Replies: 0
    Last Post: 04-01-2008, 10:17 AM
  5. Replies: 0
    Last Post: 09-28-2007, 12:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •