• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can getString method retrive integer type variables ? And what is the problem with this code?  RSS feed

 
samar das
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created a table emp10 using SQL Developer

create table emp10(
empid integer primary key,
ename varchar2(30)
);
select * from emp10;

I have attached the data in the table in form of a picture attachment





I am getting this error

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at Test.main(Test.java:6)


But my question is not only this


I have another question -  here we are retrieving the empid , which is a integer datatype variable using getString method , does this works  or we have use  getInt method ?
table.png
[Thumbnail for table.png]
run.png
[Thumbnail for run.png]
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Do you have the Oracle JDBC driver installed? And specified in your classpath?

Henry
 
samar das
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Henry Wong   i have attached a screenshot

I have copied all 3 jar files from jdbc subfolder of oraclexe  to  java > JDK >  LIB

Will that be OK ?

file.png
[Thumbnail for file.png]
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
samar das wrote:
I have copied all 3 jar files from jdbc subfolder of oraclexe  to  java > JDK >  LIB

Will that be OK ?


What is wrong with using the classpath?  It is the officially supported method, and it works well.

As for using the JRE/JDK library directory, I am sure there is a way to do that... but I believe that it is highly not recommended, and hence, I don't use it at all.

Henry
 
samar das
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Henry Wong   can you answer my other question

I have another question - here we are retrieving the empid , which is a integer datatype variable using getString method , does this works or we have use getInt method ?

 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
samar das wrote:I have another question - here we are retrieving the empid , which is a integer datatype variable using getString method , does this works or we have use getInt method ?
Yes you can retrieve int data type using getString method of ResultSet which retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language. Will be good to read getString() method and getInt() method
Since
JLS 8 5.1.11. String Conversion wrote:Any type may be converted to type String by string conversion. A value x of primitive type T is first converted to a reference value as if by giving it as an argument to an appropriate class instance creation expression. If T is byte, short, or int, then use new Integer(x). This reference value is then converted to type String by string conversion.
With getInt() you can always retrieve int value but if the column data type is varchar and you try to retrieve using getInt() method then it gives SQLException: Invalid value for getInt().

 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might be able to get a String with the same text as the number by using getString on an int column, but you probably don't want to. Remember that a String is not a number and a number is not a String.

If you go through the ResultSet documentation, it refers you to the JDBC Specification which tells you whether you can get a particular datatype from a particular method in a ResultSet.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!