Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Java
package sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ConnectionDemo {
	
	 public static void main(String[] args) {
	
		 try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			 Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","9848451415");
			 PreparedStatement st = con.prepareStatement("insert into emp(empno,ename) values(?,?)");
			 st.setInt(1, 3);
			 st.setString(2, "Naresh");
			 if(st.execute()){
				 System.out.println("Successfull executed");
			 }
			 else
				 System.out.println("Failed");
			
			 
		 }	 
		 catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		 
		 
	 } 
}


What I have tried:

I tried to insert another record into the existing table emp. Initially program is executed but my local database didnt got updated.
Posted
Updated 13-Oct-16 23:09pm
v2
Comments
David_Wimbley 23-Sep-16 14:36pm    
Did your code throw an error/exception? If so, please indicate what the entire stacktrace was by using the improve question link above.
Member 12756458 24-Sep-16 23:02pm    
i used execute(boolean) function to test whether the given sql command is right or not. It is giving me failure i.e execute function is returning false.
[no name] 23-Sep-16 15:08pm    
http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

I would personally use st.executeUpdate() which returns an integer. If the integer value is greater than 0, the statement has been executed successfully otherwise it will either be 0 or throw an error which will be caught in the catch block.
Another issue would be in database connection. Have you ensured that your connection is not null? Have you also loaded ojdbc jar file/ library to your classpath? Kindly check on the two and update us.

if(st.execute()){
System.out.println("Successfull executed");
}

would change to if(st.executeUpdate()>0){
System.out.println("Successfull executed");
}
 
Share this answer
 
According 2 my point of view, u should write ps.executeUpdate() after ps initialziation
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900