fm134
asked on
java code to insert dynamic data to oracle database
I am writing a dynamic java code that if it find certine data in my xml ducument it must compare the coulmn name in the data base with the data I found. if they match insert its data to the database
My code:
public void characters(char ch[], int start, int length)
{
String data = String.valueOf(ch, start, length).trim();
if(this.insideTag && this.prevData != null)
{
System.out.println("Data in next tag after founding '"+prevData+"': "+data);
//here I want to compare the coulmn names of my database with "prevData" if they match insert the "data" as data for this coulmn
this.prevData = null;
}
for(int i=0;i<searchData.length;i+ +)
{
if(data.equalsIgnoreCase(s earchData[ i]))
{
this.prevData = data;
break;
}
}
}
My code:
public void characters(char ch[], int start, int length)
{
String data = String.valueOf(ch, start, length).trim();
if(this.insideTag && this.prevData != null)
{
System.out.println("Data in next tag after founding '"+prevData+"': "+data);
//here I want to compare the coulmn names of my database with "prevData" if they match insert the "data" as data for this coulmn
this.prevData = null;
}
for(int i=0;i<searchData.length;i+
{
if(data.equalsIgnoreCase(s
{
this.prevData = data;
break;
}
}
}
ASKER
Hi jaxx,
thanks but I know how to connect to database now.
but the eproblem is how to
System.out.println("Data in next tag after founding '"+prevData+"': "+data);
//here I want to compare the coulmn names of my database with "prevData" if they match insert the "data" as data for this coulmn
and u gave me:
PreparedStatement prep = conn.prepareStatement("ins ert into TBL("+val+")");
prep.executeQuery();
which did not compare the coulmn name with "prevData"
thanks but I know how to connect to database now.
but the eproblem is how to
System.out.println("Data in next tag after founding '"+prevData+"': "+data);
//here I want to compare the coulmn names of my database with "prevData" if they match insert the "data" as data for this coulmn
and u gave me:
PreparedStatement prep = conn.prepareStatement("ins
prep.executeQuery();
which did not compare the coulmn name with "prevData"
Hope this helps:
private insertIntoDB(String val)
// Database Connection object
Connection conn = null;
// Set database URL details.
String url = "jdbc:oracle:thin:@localho st:1521:or cl";
try {
// Instantiate the driver class.
Class.forName("oracle.jdbc .OracleDri ver");
// Get the database connection.
conn = DriverManager.getConnectio n(url,"sys tem","pass ");
PreparedStatement prep = conn.prepareStatement("sel ect yourcol from yourtable");
ResultSet rs = prep.executeQuery();
while(rs.next()){
String dbCol = rs.getString("yourcol");
if(!val.equals(dbCol)){
PreparedStatement prep = conn.prepareStatement("ins ert into yourtable(yourcol) values('"+val+"')");
prep.executeQuery();
break;
}
}
}catch (Exception e) {
e.printStackTrace();
}
private insertIntoDB(String val)
// Database Connection object
Connection conn = null;
// Set database URL details.
String url = "jdbc:oracle:thin:@localho
try {
// Instantiate the driver class.
Class.forName("oracle.jdbc
// Get the database connection.
conn = DriverManager.getConnectio
PreparedStatement prep = conn.prepareStatement("sel
ResultSet rs = prep.executeQuery();
while(rs.next()){
String dbCol = rs.getString("yourcol");
if(!val.equals(dbCol)){
PreparedStatement prep = conn.prepareStatement("ins
prep.executeQuery();
break;
}
}
}catch (Exception e) {
e.printStackTrace();
}
Sorry,
PreparedStatement prep = conn.prepareStatement("ins ert into yourtable(yourcol) values('"+val+"')");
shud just read
prep = conn.prepareStatement("ins ert into yourtable(yourcol) values('"+val+"')");
PreparedStatement prep = conn.prepareStatement("ins
shud just read
prep = conn.prepareStatement("ins
ASKER
thanks jaxx,
but i am facing some problems:
C:\classes>javac -classpath C:\oracle\product\10.1.0\d b_1\jdbc\l ib\classes 12.jar
com\psol\xbe2\MySAXApp2.ja va
com\psol\xbe2\MySAXApp2.ja va:63: cannot resolve symbol
symbol : class PreparedStatement
location: class com.psol.xbe2.MySAXApp2
PreparedStatement prep = conn.prepareStatement("sel ect p from pro");
^
1 error
the code:
private void insertIntoDB(String val)
{
// Database Connection object
Connection conn = null;
// Set database URL details.
String url = "jdbc:oracle:thin:@localho st:1521:or cl";
try {
// Instantiate the driver class.
Class.forName("oracle.jdbc .OracleDri ver");
// Get the database connection.
conn = DriverManager.getConnectio n(url,"sys tem","pass ");
PreparedStatement prep = conn.prepareStatement("sel ect p from pro");
ResultSet rs = prep.executeQuery();
while(rs.next()){
String dbCol = rs.getString("p");
if(!val.equals(dbCol)){
prep = conn.prepareStatement("ins ert into yourtable(p) values('"+val+"')");
prep.executeQuery();
break;
}
}
}catch (Exception e) {
e.printStackTrace();
}
}
but i am facing some problems:
C:\classes>javac -classpath C:\oracle\product\10.1.0\d
com\psol\xbe2\MySAXApp2.ja
com\psol\xbe2\MySAXApp2.ja
symbol : class PreparedStatement
location: class com.psol.xbe2.MySAXApp2
PreparedStatement prep = conn.prepareStatement("sel
^
1 error
the code:
private void insertIntoDB(String val)
{
// Database Connection object
Connection conn = null;
// Set database URL details.
String url = "jdbc:oracle:thin:@localho
try {
// Instantiate the driver class.
Class.forName("oracle.jdbc
// Get the database connection.
conn = DriverManager.getConnectio
PreparedStatement prep = conn.prepareStatement("sel
ResultSet rs = prep.executeQuery();
while(rs.next()){
String dbCol = rs.getString("p");
if(!val.equals(dbCol)){
prep = conn.prepareStatement("ins
prep.executeQuery();
break;
}
}
}catch (Exception e) {
e.printStackTrace();
}
}
Did u add the import statement for the PreparedStatement ?
- import java.sql.PreparedStatement ;
- import java.sql.PreparedStatement
ASKER
thanks jaax u solved it.
but I have two more questions:
1- C:\classes>javac -classpath . com\psol\xbe2\MySAXApp2.ja va
com\psol\xbe2\MySAXApp2.ja va:11: package oracle.jdbc does not exist
import oracle.jdbc.OraclePrepared Statement;
^
1 error
I have this erroe even if put the C:\oracle\product\10.1.0\d b_1\jdbc\l ib\classes 12.jar in my classpath???
2- how I call the code private void insertIntoDB(String val)
from another function??
but I have two more questions:
1- C:\classes>javac -classpath . com\psol\xbe2\MySAXApp2.ja
com\psol\xbe2\MySAXApp2.ja
import oracle.jdbc.OraclePrepared
^
1 error
I have this erroe even if put the C:\oracle\product\10.1.0\d
2- how I call the code private void insertIntoDB(String val)
from another function??
1. You need not specify the vendor specific PreparedStatement. java.sql.PreparedStatement will do.
In this way, you would make ur code independent of DB Drivers. Class.forName will load the appropriate DB Driver. classes12.jar shud in the classpath as you have done.
2.Since the calling method is in the same class, you can. 'private' has been specified to disallow access of this method from other class
In this way, you would make ur code independent of DB Drivers. Class.forName will load the appropriate DB Driver. classes12.jar shud in the classpath as you have done.
2.Since the calling method is in the same class, you can. 'private' has been specified to disallow access of this method from other class
ASKER
thanks jaax,
but can u please change the the coulmn name can be dynamic
I have to compare every coulmn name with the data I found if they match or simematch then insert the data to that coulm.
PreparedStatement prep = conn.prepareStatement("sel ect(dynami c name) from pro");
but can u please change the the coulmn name can be dynamic
I have to compare every coulmn name with the data I found if they match or simematch then insert the data to that coulm.
PreparedStatement prep = conn.prepareStatement("sel
ASKER
hi agian
I tried the code and nothing inserted in the database!!
private void insertIntoDB(String val)
{
// Database Connection object
Connection conn = null;
// Set database URL details.
String url = "jdbc:oracle:thin:@localho st:1521:or cl";
try {
// Instantiate the driver class.
Class.forName("oracle.jdbc .OracleDri ver");
// Get the database connection.
conn = DriverManager.getConnectio n(url,"sys tem","pass ");
PreparedStatement prep = conn.prepareStatement("sel ect p from pro1");
ResultSet rs = prep.executeQuery();
while(rs.next()){
String dbCol = rs.getString("p");
if(!val.equals(dbCol)){
prep = conn.prepareStatement("ins ert into pro1(p) values('"+val+"')");
prep.executeQuery();
break;
}
}
}catch (Exception e) {
e.printStackTrace();
}
}
public void characters(char ch[], int start, int length)
{
String data = String.valueOf(ch, start, length).trim();
insertIntoDB("xxx");
}
I tried the code and nothing inserted in the database!!
private void insertIntoDB(String val)
{
// Database Connection object
Connection conn = null;
// Set database URL details.
String url = "jdbc:oracle:thin:@localho
try {
// Instantiate the driver class.
Class.forName("oracle.jdbc
// Get the database connection.
conn = DriverManager.getConnectio
PreparedStatement prep = conn.prepareStatement("sel
ResultSet rs = prep.executeQuery();
while(rs.next()){
String dbCol = rs.getString("p");
if(!val.equals(dbCol)){
prep = conn.prepareStatement("ins
prep.executeQuery();
break;
}
}
}catch (Exception e) {
e.printStackTrace();
}
}
public void characters(char ch[], int start, int length)
{
String data = String.valueOf(ch, start, length).trim();
insertIntoDB("xxx");
}
ASKER
thanks jax there is no problem now:)
Cool Man. Please award me some points ;)
ASKER
I just have one problem
can u please change the the coulmn name can be dynamic
I have to compare every coulmn name with the data I found if they match or simematch then insert the data to that coulm.
PreparedStatement prep = conn.prepareStatement("sel ect(dynami c name) from pro");
can u please change the the coulmn name can be dynamic
I have to compare every coulmn name with the data I found if they match or simematch then insert the data to that coulm.
PreparedStatement prep = conn.prepareStatement("sel
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks alooot Jaax
You're welcome !
private insertIntoDB(String val)
// Database Connection object
Connection conn = null;
// Set database URL details.
String url = "jdbc:oracle:thin:@localho
try {
// Instantiate the driver class.
Class.forName("oracle.jdbc
// Get the database connection.
conn = DriverManager.getConnectio
PreparedStatement prep = conn.prepareStatement("ins
prep.executeQuery();
}catch (Exception e) {
e.printStackTrace();
}