|
|||||
|
|
#1 |
|
|
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 cl*** user has to run this cl*** 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.*; cl*** 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 Cl***.forName("sun.jdbc.odbc.JdbcOdbcDriver").newI nstance(); } catch (Cl***NotFoundException 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 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[i] = br.readLine(); System.out.println("Please enter Employee work hours"); emphours[i] = br.readLine(); System.out.println("Please enter Employee Hourly pay"); emppay[i] = br.readLine(); db_statement.executeUpdate ("insert into practice2 values('empname[i]','emphours[i]','emppay[i]')"); //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) { } } } |
|
|
#2 |
|
|
> I am new to java and student. A better group for people starting in Java programming is described here. <http://www.physci.org/codes/javafaq.jsp#cljh> > ..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 do***enting your program if nothing more. HTH |
|
|
#3 |
|
|
<snip> > db_statement.executeUpdate ("insert into practice2 > values('empname[i]','emphours[i]','emppay[i]')"); db_statement.executeUpdate( "insert into practice2 values ( " + empname[i] + ", " + emphours[i] + ", " + emppay[i] + " )" ); |
|
|
#4 |
|
|
Sudsy wrote:
> > Amit wrote: > <snip> > > db_statement.executeUpdate ("insert into practice2 > > values('empname[i]','emphours[i]','emppay[i]')"); > > db_statement.executeUpdate( "insert into practice2 values ( " + > empname[i] + ", " + emphours[i] + ", " + emppay[i] + " )" ); Certainly closer than the OP, but I would ***ume single quotes were needed around the name: db_statement.executeUpdate( "insert into practice2 values ( '" + empname[i] + "', " + emphours[i] + ", " + emppay[i] + " )" ); .... also ***uming hours and pay are numeric values not requiring quoting. -- Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com) ================================================== ============ * The Ultimate DBMS is here! * FirstSQL/J Object/Relational DBMS (http://www.firstsql.com) |
|
|
#5 |
|
|
Lee Fesperman wrote:
<snip> > Certainly closer than the OP, but I would ***ume single quotes were needed around the > name: > > db_statement.executeUpdate( "insert into practice2 values ( '" + > empname[i] + "', " + emphours[i] + ", " + emppay[i] + " )" ); > > ... also ***uming hours and pay are numeric values not requiring quoting. > Doh! I shouldn't have ***umed 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. |
|
|
#6 |
|
|
On Mon, 6 Sep 2004 01:05:24 +0800, Sudsy wrote
(in article <413B4754.6020400@hotmail.com>): > Amit wrote: > <snip> >> db_statement.executeUpdate ("insert into practice2 >> values('empname[i]','emphours[i]','emppay[i]')"); > > db_statement.executeUpdate( "insert into practice2 values ( " + > empname[i] + ", " + emphours[i] + ", " + emppay[i] + " )" ); > this will break if there are any nested ' or " in any of these fields. ESP. empname. |
|
|
#7 |
|
|
Sudsy wrote:
> > Lee Fesperman wrote: > > > > db_statement.executeUpdate( "insert into practice2 values ( '" + > > empname[i] + "', " + emphours[i] + ", " + emppay[i] + " )" ); > > > > ... also ***uming hours and pay are numeric values not requiring quoting. > > > > Doh! I shouldn't have ***umed 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. -- Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com) ================================================== ============ * The Ultimate DBMS is here! * FirstSQL/J Object/Relational DBMS (http://www.firstsql.com) |
|
|
#8 |
|
|
Sudsy <bitbucket44@hotmail.com> writes:
> Doh! I shouldn't have ***umed 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[i]); db_statement.setString(2, emphours[i]); db_statement.setString(3, emppay[i]); 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. ![]() |