Help in JDBC

A

Amit

Hello friends!!

I am new to java and student. I have worte a program in java but some
reason it is not working the way it should do. it is not giving any
error but when i run it is not working properly. so, please help me
out. I have tried so many times to fix it but i could not fix.

Thank you very much


amit

//Here is the code

//main class user has to run this class to get output

import java.io.*;
import java.sql.*;
//import javax.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.lang.String;
import java.lang.*;

class SalaryCount
{
public static void main(String args[])
{
try {

int c = 2;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
//System.out.println("Please enter number of employee you want to
enter");

//int c = (char) br.read();
//int c = (int) ca;

String empname[] = new String[c];
String emphours[] = new String[c];
String emppay[] = new String[c];
String res[] = new String[c];






//Attempt to load database driver

try
{
// Load Sun's jdbc-odbc driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
}
catch (ClassNotFoundException cnfe) // driver not found
{
System.err.println ("Unable to load database driver");
System.err.println ("Details : " + cnfe);
System.exit(0);
}


// Create a URL that identifies database
String url = "jdbc:eek:dbc:COMPUTERUSER/practice/practice2" + args[0];

// Now attempt to create a database connection
Connection db_connection =
DriverManager.getConnection ("url", "amit", "amit");


// Create a statement to send SQL
Statement db_statement = db_connection.createStatement();





for(int i=0; i<c; i++)
{
double value;
System.out.println("Please enter Employee
Name");
empname = br.readLine();


System.out.println("Please enter Employee work
hours");
emphours = br.readLine();
System.out.println("Please enter Employee
Hourly pay");
emppay = br.readLine();


db_statement.executeUpdate ("insert into practice2
values('empname','emphours','emppay')");


//mul ob = new mul();
}

for(int j=0; j<c; j++) {
//res[j] = ob.multiply(emphours[j],
emppay[j]);

System.out.println("Employee Name: " +
empname[j]);
System.out.println("Employee Hours: " +
emphours[j]);
System.out.println("Employee Salary per hour: " +
emppay[j]);
System.out.println("Employee total salary:"
+res[j]);
System.out.println(" ");

}



}catch (Exception e) { }

}
}
 
A

Andrew Thompson

I am new to java and student.

A better group for people starting in
Java programming is described here.
..I have worte a program in java but some
reason it is not working the way it should do. it is not giving any
error but when i run it is not working properly.

Normally I cut in at this point to suggest
you be more specific, but I can see from
your code why you cannot be so. *
..so, please help me out.

I recommend you drop these 'personal' appeals.
If somone replies to your problem, they reply,
asking them 'please' will hardly make a difference.
}catch (Exception e) { }

* This single line is why you you are not
getting proper error ouput, that is very bad
practice. Instead I suggest..

} catch (Exception e) {
e.printStackTrace();
}

Note that you should be declaring more
specific Exceptions to catch, for the
purposes of documenting your program
if nothing more.

HTH
 
S

Sudsy

Amit wrote:
db_statement.executeUpdate ("insert into practice2
values('empname','emphours','emppay')");


db_statement.executeUpdate( "insert into practice2 values ( " +
empname + ", " + emphours + ", " + emppay + " )" );
 
L

Lee Fesperman

Sudsy said:
Amit wrote:
db_statement.executeUpdate ("insert into practice2
values('empname','emphours','emppay')");


db_statement.executeUpdate( "insert into practice2 values ( " +
empname + ", " + emphours + ", " + emppay + " )" );


Certainly closer than the OP, but I would assume single quotes were needed around the
name:

db_statement.executeUpdate( "insert into practice2 values ( '" +
empname + "', " + emphours + ", " + emppay + " )" );

.... also assuming hours and pay are numeric values not requiring quoting.
 
S

Sudsy

Lee Fesperman wrote:
Certainly closer than the OP, but I would assume single quotes were needed around the
name:

db_statement.executeUpdate( "insert into practice2 values ( '" +
empname + "', " + emphours + ", " + emppay + " )" );

... also assuming hours and pay are numeric values not requiring quoting.


Doh! I shouldn't have assumed facts not in evidence. Turns out that
they're all String so should, of course, be quoted. Then again, we
don't have access to the DDL or table meta-data so some of these
fields could actually be numeric. If that's the case then the OP
will have to get familiar with Integer#parseInt and kin. Numeric
fields, as Lee pointed out, don't require quoting.
 
S

steve

Amit wrote:
db_statement.executeUpdate ("insert into practice2
values('empname','emphours','emppay')");


db_statement.executeUpdate( "insert into practice2 values ( " +
empname + ", " + emphours + ", " + emppay + " )" );


this will break if there are any nested ' or " in any of these fields.
ESP. empname.
 
L

Lee Fesperman

Sudsy said:
Lee said:
db_statement.executeUpdate( "insert into practice2 values ( '" +
empname + "', " + emphours + ", " + emppay + " )" );

... also assuming hours and pay are numeric values not requiring quoting.


Doh! I shouldn't have assumed facts not in evidence. Turns out that
they're all String so should, of course, be quoted. Then again, we
don't have access to the DDL or table meta-data so some of these
fields could actually be numeric. If that's the case then the OP
will have to get familiar with Integer#parseInt and kin. Numeric
fields, as Lee pointed out, don't require quoting.


Some DBMSs will convert it anyway.
 
T

Tor Iver Wilhelmsen [TeamB]

Sudsy said:
Doh! I shouldn't have assumed facts not in evidence. Turns out that
they're all String so should, of course, be quoted.

Actually, performing JDBC statements in a loop should be done using
PreparedStatement for efficiency reasons, and that will take care of
any necessary quoting as well. A third advantage is that it will
escape ' etc. for you.

PreparedStatement db_statement = db_connection.prepareStatement(
"insert into practice2 values (?,?,?)");
for(int i=0; i<c; i++) {
db_statement.setString(1, empname);
db_statement.setString(2, emphours);
db_statement.setString(3, emppay);
db_statement.executeUpdate();
}
db_statement.close();

That said, constructing SQL statements with concatenation is evil
anyway, because it's open to security attacks.

So: Preparedstatement 4 - Statement 0. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top