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

[410 byte] By [GloomyProgrammera] at [2007-11-27 5:13:27]
# 1
this is not working for me ....String t= "INSERT into test values (?,?)";PreparedStatement ps = connection.prepareStatement(t);for (int n = 0; n < 10; n++) { ps.setInt(n);}tasunny
GloomyProgrammera at 2007-7-12 10:35:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2
>ps.setInt(n);This method does not exist. Try using a method that exists.
cotton.ma at 2007-7-12 10:35:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 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

GloomyProgrammera at 2007-7-12 10:35:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
> pls check the below program .....> > setInt(n); still exists......> >Where?Hint: no, it doesn't. setInt(int, int) exists. setInt(int) does not exist.
cotton.ma at 2007-7-12 10:35:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

Try to read the API documentation before going brainlessly coding ;)

Here is the PreparedStatement API documentation: http://java.sun.com/j2se/1.5.0/docs/api/java/sql/PreparedStatement.html

It shows you which methods are available to you.

Also the official Sun JDBC tutorial might be very useful:

http://java.sun.com/docs/books/tutorial/jdbc/TOC.html

http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html

BalusCa at 2007-7-12 10:35:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6
You forgot to follow the actual syntax of the PreparedStatement's methods .So find the following snippetpst=connection.prepareStatement("insert into tempA values(?)");for(int i=0;i<10;i++){pst.setInt(1,i);pst.executeUpdate();}
hari_honeya at 2007-7-12 10:35:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 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

GloomyProgrammera at 2007-7-12 10:35:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8
> can some one find the dug for me ?Yes. You haven't clicked at the links to the PreparedStatement tutorial.
BalusCa at 2007-7-12 10:35:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 9
many thnx
GloomyProgrammera at 2007-7-12 10:35:02 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...