• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC and database

 
Jasbir Dhaliwal
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am having some problems and I hope any one out there could help me.
I am developing a system that is connected to an Oracle database using JDBC.
However, when i need to insert value to the database , i need to explicitly say the data.

E.g ;
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc racle:thin:@myServer:1521:mydatabse";
String user="Scot";
String password="Scot";
String name="Jasmin";
Connection con=DriverManager.getConnection(url,user,password);
Statement statement=con.createStatement();
String sql=" Insert into myTable"+"(NAME)"+ "Values ('" name"');
ResultSet result=statement.executeUpdate(sql);
What do i need to do so that user can key in the data? Do i create constructor, use set or get functions??
Please help,I am a bit confuse.
Thanks
 
Ryan Bailey
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jasbir,
Try the following:

Use a PreparedStatement instead of a Statement. PreparedStatements are precompiled and therefore execute a lot faster. I would recommend doing it this way. I don't know what type of business logic you have. The best way to pass your variables into the sql function would to set them into a bean and pass the bean into either the constructor or this method. This way if you modify your code down the road you won't have to go back and adjust this code since all varaibles will be in a bean object. After calling PreparedStatement pstmnt = con.prepareStatement(sql); make sure you use the appropriate stmnt.set**** methods depending on what type of a primative type or object you are setting.
Good luck and let me know if you have more questions,
Ryan
 
chanoch wiggers
Author
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the other thing about prepared statements is that they are generally more secure to use
 
Jasbir Dhaliwal
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI:
thanks for the help .
Jasbir
 
Jasbir Dhaliwal
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am facing another prob, I hope anyone is able to help me.
I could connect to jdbc driver in application but when i converted the code to applet, I had the following error:
java.lang.classnotfoundexception racle.jdbc.driver.OracleDriver.
I think the reason for this is that the classpath such as c:\[oracle_home]\jdbc\lib\classes12.zip;c:\[oracle_home]\jdbc\lib\nls_charset12.zip;
is known to the application but it is not known to the applet.
So, i think , i need to include it the html file, but how do i do it?
I did some research , and i found out that the codebase attribute need to point to a directory containing the java classes from the Thin JDBC distribution *and the compiled jdbcapplet.class.
how do i do it? Could anyone help me? I have already spent one week to figure it out and could not?
Jasbir
 
Amol Desai
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
You can set the classpath for your applet using
<Applet
code = Applet.class
archive = "c:\oracle_home\...\classes12.zip,c:\...\nls_charset12.zip"
width = 200
height = 100
>

Hope this helps
 
Jasbir Dhaliwal
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi:
thanks Amol, I would try that as soon as I get to work tomm.
Best Regards,
Jasbir
 
Jasbir Dhaliwal
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help but i still get the same error java.lang.ClassNotFoundException racle:jdbc.driver.OracleDriver
Thanks in advance,
Jasbir
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic