jQuery in Action, 2nd edition*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "JDBC and database" Watch "JDBC and database" New topic
Author

JDBC and database

Jasbir Dhaliwal
Greenhorn

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 ;
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

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,
Ryan


SCJP 1.4, SCWCD
Java: The power, elegance, and simplicity of a hand grenade
chanoch wiggers
Author
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="http://www.amazon.com/exec/obidos/ASIN/1861007736/" target="_blank" rel="nofollow">Author of Professional Apache Tomcat</a></p>
Jasbir Dhaliwal
Greenhorn

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

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\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

Joined: Jan 29, 2003
Posts: 82
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

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

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,
Jasbir
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDBC and database
 
Similar Threads
EJB 3 Stateful BMT SessionBean Open Transaction not behaving properly
Stateless EJB and transactions..pls help
Invalid Object Name
Query Correct, but not functioning
Connection with database Help