LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-03-2004, 03:32 AM   #1
keikun_naruchan
LQ Newbie
 
Registered: Feb 2004
Distribution: Suse, LFS(Linux From Scratch)
Posts: 20

Rep: Reputation: 0
Exclamation JDBC PROBLEM (JAVA using JCreator)


m working on a Java code that connects to a access database thru odbc.. I would like to be able to modify the access database via java.. to edit fields.. to add new records.. to delete.. and to simply view data..

here is my code and currently im having a problem in reading input from keyboard and comparing the input stored in avariable.. could you help me out..

HERE is my complete code..

import java.sql.*;
import java.io.*;
public class JDBCtest
{
public static void main(String[] args) throws IOException
{
BufferedReader stdin = new BufferedReader
(new InputStreamReader(System.in));

String woodtype;

try
{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String dataSourceName = "empTEST";
String dbURL = "jdbcdbc:" + dataSourceName;
Connection con = DriverManager.getConnection(dbURL, "","");
Statement s = con.createStatement();
System.out.println("Employee Records Management" + '\n');
System.out.println("What do you wish to do?");
System.out.println("Legend:");
System.out.println("To View Records: Enter v or V");
System.out.print("Enter Choice:");
//I THINK THIS IS WHERE MY PROBLEM STARTS
woodtype = stdin.readLine();
if(woodtype == "v" || woodtype == "V")
{
System.out.println("hello");
//Statement a = con.createStatement();
//s.execute("create table TEST12345 ( column_name integer )"); // create a table
//s.execute("insert into TEST12345 values(1)"); // insert some data into the table
//s.execute("select column_name from TEST12345"); // select the data from the table
s.execute("select * from Employee");
//s.execute("select emp_title from Employee");
ResultSet rs = s.getResultSet(); // get any ResultSet that came from our query
//ResultSet rs2 = a.getResultSet();

if (rs != null) // if rs == null, then there is no ResultSet to view

while ( rs.next() ) // this will step through our data row-by-row

{
/* the next line will get the first column in our current row's ResultSet
as a String ( getString( columnNumber) ) and output it to the screen */
System.out.println("Data from Employee Number: " + rs.getString(1) );
System.out.println("Data from Employee Name: " + rs.getString(2) );
System.out.println("Data from Employee Title: " + rs.getString(3) );
System.out.println("Data from Employee Department: " + rs.getString(4) );
System.out.println("Data from Employee Contact Number: " + rs.getString(5) + '\n');
//System.out.println ("You entered: \"" + woodtype + "\"");
//System.out.println("Data from Employee Number: " + rs.getString(6) );

}
//s.execute("drop table TEST12345");
s.close(); // close the Statement to let the database know we're done with it
con.close(); // close the Connection to let the database know we're done with it
}
}


catch (Exception err) {
System.out.println("ERROR: " + err);
}

}
}


Thanks a lot for any help or suggestion you could give me..
LOOK FORWARD TO YOUR REPLY
 
Old 06-03-2004, 05:47 AM   #2
dave_starsky
Member
 
Registered: Oct 2003
Location: UK, Manchester
Distribution: Gentoo (2.6.10-r4) & Ubuntu
Posts: 145

Rep: Reputation: 16
Code:
woodtype = stdin.readLine();
if(woodtype == "v" || woodtype == "V")
Since woodtype is a string you need this:

Code:
if(woodtype.equalsIgnoreCase("v"))
this will check for "v" and "V"
 
Old 06-03-2004, 07:14 PM   #3
keikun_naruchan
LQ Newbie
 
Registered: Feb 2004
Distribution: Suse, LFS(Linux From Scratch)
Posts: 20

Original Poster
Rep: Reputation: 0
THANKS A LOT dave_starsky

YOU ARE SO COOL.. THANKS.. UR A JAVA MASTER.. THANKS AGAIN..
 
Old 06-04-2004, 02:02 AM   #4
keikun_naruchan
LQ Newbie
 
Registered: Feb 2004
Distribution: Suse, LFS(Linux From Scratch)
Posts: 20

Original Poster
Rep: Reputation: 0
problem2

I just modified my code abit and now ..
Now im having a problem in deleting a record..

HOPE you could help me.. THANKS for any sugestion you could give me..

import java.sql.*;
import java.io.*;
public class JDBCtest2
{
public static void Selection()
{
System.out.println("***********Employee Records Management************" + '\n');
System.out.println("What do you wish to do?");
System.out.println("Legend:");
System.out.println("To View Records: Enter v or V");
System.out.println("To Delete Records: Enter d or D");
System.out.println("To Quit Program: Enter q or Q");
System.out.print("Enter Choice:");
}
public static void main(String[] args) throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
char input;
String INPUT, record_num;
int x = 0, numinput;

try
{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String dataSourceName = "empTEST2";
String dbURL = "jdbcdbc:" + dataSourceName;
Connection con = DriverManager.getConnection(dbURL, "","");
Statement s = con.createStatement();

Selection();
//choice = stdin.readLine();
input = (char)System.in.read();
INPUT = in.readLine();
while(input!='Q' || input != 'q')
{
//if(choice.equalsIgnoreCase("v"))
if(input == 'v' || input == 'V')
{
System.out.println("");
s.execute("select * from employee");
//s.execute("select emp_title from Employee");
ResultSet rs = s.getResultSet();

// if rs == null, then there is no ResultSet to view
if (rs != null)

// this will step through our data row-by-row
while ( rs.next() )
{
//OUTPUT TO SCREEN
x=x+1;
System.out.println("Record Number: " + x );
System.out.println("Data from Employee Number: " + rs.getString(1) );
System.out.println("Data from Employee Name: " + rs.getString(2) );
System.out.println("Data from Employee Title: " + rs.getString(3) );
System.out.println("Data from Employee Department: " + rs.getString(4) );
System.out.println("Data from Employee Contact Number: " + rs.getString(5) + '\n');
}

System.out.println("End of Record");
System.out.println("-----------------------------------");
x=0;
Selection();
input = (char)System.in.read();
INPUT = in.readLine();

//break;
}
else if(input == 'd' || input == 'D')
{
System.out.print("Enter the Record Number you wish to delete: ");

record_num = in.readLine();
numinput = Integer.parseInt(record_num);
System.out.println("Input = " + numinput);
s.execute("DELETE FROM employee where emp_id = " + numinput);
ResultSet rs = s.getResultSet();
if (rs != null)
while ( rs.next() )
{
//OUTPUT TO SCREEN
x=x+1;
System.out.println("Record Number: " + x );
System.out.println("Data from Employee Number: " + rs.getString(1) );
System.out.println("Data from Employee Name: " + rs.getString(2) );
System.out.println("Data from Employee Title: " + rs.getString(3) );
System.out.println("Data from Employee Department: " + rs.getString(4) );
System.out.println("Data from Employee Contact Number: " + rs.getString(5) + '\n');
}

System.out.println("End of Record");
System.out.println("-----------------------------------");
x=0;

Selection();
input = (char)System.in.read();
INPUT = in.readLine();
}
else if(input == 'q' || input == 'Q')
{
System.out.println('\n' + "EXITING PROGRAM..." + '\n');
System.exit(0);
}
else
{
System.out.println('\n' + "WARNING!!!" + '\n' + "You have entered incorrect command!" + '\n');
Selection();
input = (char)System.in.read();
INPUT = in.readLine();

}

}
// close the Statement to let the database know we're done with it
s.close();
// close the Connection to let the database know we're done with it
con.close();
}

catch (Exception err)
{
System.out.println("WARNING!!!: " + err);
//System.exit(0);
}

}
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java JDBC driver mickeyboa Fedora 2 07-23-2005 07:53 AM
jdbc connection in java balanagireddy Programming 2 07-01-2004 06:42 AM
JDBC connection in java balanagireddy Programming 1 06-30-2004 07:34 AM
I was working with jdbc in java in which we need navalarora Linux - Software 0 12-05-2003 06:41 AM
java.lang.NullPointerException in JDBC test chr15t0 Programming 2 07-07-2003 11:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration