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.
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
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
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>
Joined: Mar 04, 2003
HI: thanks for the help . Jasbir
Joined: Mar 04, 2003
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