This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Servlets and the fly likes JDBC and database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Servlets
Bookmark "JDBC and database" Watch "JDBC and database" New topic

JDBC and database

Jasbir Dhaliwal

Joined: Mar 04, 2003
Posts: 12
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 ;
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.
Ryan Bailey
Ranch Hand

Joined: Feb 26, 2002
Posts: 134
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,

Java: The power, elegance, and simplicity of a hand grenade
chanoch wiggers
Ranch Hand

Joined: May 24, 2001
Posts: 245
the other thing about prepared statements is that they are generally more secure to use

chanoch<p><a href="" target="_blank" rel="nofollow">Author of Professional Apache Tomcat</a></p>
Jasbir Dhaliwal

Joined: Mar 04, 2003
Posts: 12
thanks for the help .
Jasbir Dhaliwal

Joined: Mar 04, 2003
Posts: 12
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\;c:\[oracle_home]\jdbc\lib\;
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?
Amol Desai
Ranch Hand

Joined: Jan 29, 2003
Posts: 82
You can set the classpath for your applet using
code = Applet.class
archive = "c:\oracle_home\...\,c:\...\"
width = 200
height = 100

Hope this helps
Jasbir Dhaliwal

Joined: Mar 04, 2003
Posts: 12
thanks Amol, I would try that as soon as I get to work tomm.
Best Regards,
Jasbir Dhaliwal

Joined: Mar 04, 2003
Posts: 12
Thanks for the help but i still get the same error java.lang.ClassNotFoundException racle:jdbc.driver.OracleDriver
Thanks in advance,
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: JDBC and database
It's not a secret anymore!