JDBC INSERT PROGRAM
Hi All ,
I am new to JDBC Programming ....
can someone provide me ...a JDBC insert program in which i insert data through variables .......(i.e i do not know the data ....it is captured from user input ...and then place in the insert statement....
like
insert into emp values (var1,var2....etc .);
this variables are simplet integers and varchars ...
ta,
sunny
# 3
pls check the below program .....
setInt(n); still exists......
import java.sql.*;
public class InsertExample {
public static void main(String[] args)
throws SQLException {
int ret_code;
Connection conn = null;
try {
int i_empno[] = {1001, 1002, 7788};
String i_ename[] = {"JOHN","DAVID","ORATEST"};
String i_job[] = {"MANAGER","ANALYST","CLERK"};
int i_mgr[] = {7839, 1001, 1002};
String i_hiredate = "01-JAN-01";
float i_sal[] = {10000,6000, 4000};
float i_comm[] = {2000,1000,500};
int i_deptno = 10;
//Load and register Oracle driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
//Establish a connection
conn = DriverManager.getConnection("jdbc:oracle:thin:@training:1521:
Oracle", "oratest", "oratest");
String sql1 = "SELECT empno FROM emp WHERE empno = ?" ;
String sql2 = "INSERT INTO emp VALUES (?,?,?,?,?,?,?,?)";
PreparedStatement pstmt1 = conn.prepareStatement(sql1);
PreparedStatement pstmt2 = conn.prepareStatement(sql2);
for (int idx=0;idx<3;idx++)
{
pstmt1.setInt(1, i_empno[idx]);
ResultSet rset = pstmt1.executeQuery();
if (rset.next()) {
System.out.println("The employee "
+i_empno[idx]+" already exists.");
rset.close();
}
else
{
pstmt2.setInt(1, i_empno[idx]);
pstmt2.setString(2, i_ename[idx]);
pstmt2.setString(3, i_job[idx]);
pstmt2.setInt(4, i_mgr[idx]);
pstmt2.setString(5, i_hiredate);
pstmt2.setFloat(6, i_sal[idx]);
pstmt2.setFloat(7, i_comm[idx]);
pstmt2.setInt(8, i_deptno);
pstmt2.executeUpdate(); }
} // End of for loop
pstmt1.close();
pstmt2.close();
conn.close();
} catch (SQLException e) {ret_code = e.getErrorCode();
System.err.println(ret_code + e.getMessage()); conn.close();}
}
}UPDATE and DELETE operations are
# 7
can some one find the dug for me ?
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Insert_data_Jdbc
{
public static void main(String args[])
{
Connection connection = null;
Statement stmt = null;
//ResultSet rs = null;
System.out.println("I am in main");
try
{
// Load the JDBC driver
String driverName = "oracle.jdbc.driver.OracleDriver";
Class.forName(driverName).newInstance();
// Create a connection to the database
String serverName = "127.0.0.1";
String portNumber = "1521";
String sid = "oraval";
String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
String username = "scott";
String password = "tiger";
connection = DriverManager.getConnection(url, username, password);
//stmt = connection.createStatement();
//int i = 20;
String t= "INSERT into test values (?,?)";
PreparedStatement ps = connection.prepareStatement(t);
for (int n = 0; n < 10; n++)
{
ps.setInt(n,n);
}
}
catch (ClassNotFoundException e)
{
// Could not find the database driver
e.printStackTrace();
}
catch (SQLException e) {
// Could not connect to the database
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
finally
{
//release resources
/* if(rs!=null){
try {
// rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} */
if(stmt!=null){
try {
stmt.close();
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
ta
sunny