By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,654 Members | 1,517 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,654 IT Pros & Developers. It's quick & easy.

how to remove error ocurred during unserting values in various tables of database fau

P: 3
[Z{Moderator's Edit:
{Q:} how to remove error ocurred during unserting values in various tables of database fau
{Originally posted to the VBA/Access - Moved to JAVA}]

Expand|Select|Wrap|Line Numbers
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package data;
  6.  
  7. import java.sql.*;
  8.  
  9. class Data
  10. {
  11.  public static void main( String args[] )
  12.  {
  13.   try
  14.      {
  15.       // Load the database driver
  16.       Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ) ;
  17.  
  18.       // Get a connection to the database
  19.      // Connection conn = DriverManager.getConnection( "jdbc:odbc:faultmatrix" ) ;
  20. String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + "C:\\faultmatrix.accdb";
  21. Connection conn = DriverManager.getConnection(url) ;     
  22. // Print all warnings
  23.       for( SQLWarning warn = conn.getWarnings(); warn != null; warn = warn.getNextWarning() )
  24.          {
  25.           System.out.println( "SQL Warning:" ) ;
  26.           System.out.println( "State  : " + warn.getSQLState()  ) ;
  27.           System.out.println( "Message: " + warn.getMessage()   ) ;
  28.           System.out.println( "Error  : " + warn.getErrorCode() ) ;
  29.          }
  30.  
  31.       // Get a statement from the connection
  32.       Statement stmt = conn.createStatement() ;
  33.  
  34. stmt.executeUpdate("INSERT INTO faultmatrix " + "VALUES (0, 1, 0, 0 )");
  35. stmt.executeUpdate("INSERT INTO faultmatrix " + "VALUES (1, 0, 1, 1 )");
  36. stmt.executeUpdate("INSERT INTO faultmatrix " + "VALUES (2, 1, 1, 0 )");
  37.  
  38.        Statement stmt1 = conn.createStatement() ;
  39.        stmt1.executeUpdate("INSERT INTO severity " + "VALUES (0, 1 )");
  40.          stmt1.executeUpdate("INSERT INTO severity " + "VALUES (1, 1 )");
  41.            stmt1.executeUpdate("INSERT INTO severity " + "VALUES (2, 1 )");
  42.  
  43.            Statement stmt2 = conn.createStatement() ;
  44.            stmt2.executeUpdate("INSERT INTO cost " + "VALUES (0, 1)");
  45.            stmt2.executeUpdate("INSERT INTO cost " + "VALUES (1, 2)");
  46.            stmt2.executeUpdate("INSERT INTO cost " + "VALUES (2, 1)");
  47.  
  48.       // Execute the query
  49.       ResultSet rs = stmt.executeQuery( "SELECT * FROM faultmatrix" ) ;
  50.  
  51.       // Loop through the result set
  52.       while( rs.next() )
  53.       {
  54.          int id  = rs.getInt("id");
  55.          int fault0 = rs.getInt("fault0");
  56.          int fault1 = rs.getInt("fault1");
  57.          int fault2 = rs.getInt("fault2");
  58.          System.out.println(id + "-" +fault0 + "-" +fault1+ "-" +fault2);
  59.          //System.out.println(fault0);
  60.       }
  61.       rs.close();
  62.       ResultSet rs1 = stmt1.executeQuery( "SELECT * FROM severity" ) ;
  63.       while( rs1.next() )
  64.       {
  65.          int id  = rs1.getInt("ID");
  66.          int severity = rs1.getInt("severity");
  67.         System.out.println(id + "-" +severity);
  68.  
  69.       }
  70.       rs1.close();
  71.  
  72.      ResultSet rs2 = stmt2.executeQuery( "SELECT * FROM cost" ) ;
  73.        while( rs2.next() )
  74.       {
  75.          int id  = rs2.getInt("ID");
  76.          int test_cost = rs2.getInt("testcase_cost");
  77.               System.out.println(id + "-" +test_cost);
  78.       }
  79.  
  80.      rs2.close();
  81.       // Close the result set, statement and the connection
  82.  
  83.  
  84.  
  85.       stmt.close() ;
  86.       conn.close() ;
  87.      }
  88.   catch( SQLException se )
  89.      {
  90.       System.out.println( "SQL Exception:" ) ;
  91.  
  92.       // Loop through the SQL Exceptions
  93.       while( se != null )
  94.          {
  95.           System.out.println( "State  : " + se.getSQLState()  ) ;
  96.           System.out.println( "Message: " + se.getMessage()   ) ;
  97.           System.out.println( "Error  : " + se.getErrorCode() ) ;
  98.  
  99.           se = se.getNextException() ;
  100.          }
  101.      }
  102.   catch( Exception e )
  103.      {
  104.       System.out.println( e ) ;
  105.      }
  106.  }
  107. }
Oct 11 '13 #1
Share this Question
Share on Google+
5 Replies


Rabbit
Expert Mod 10K+
P: 12,421
Please use code tags when posting code or formatted data.

This doesn't look like VBA code, you are in the wrong forum. Let us know what language this is and we can move it to the right forum. It would also help to know the full error text and the line that triggers the error. You give almost no information with which to diagnose the issue.
Oct 11 '13 #2

zmbd
Expert Mod 5K+
P: 5,397
import java.sql.*;
url = "jdbc:odbc:
These look like the JDBC/JAVA Script constructs I've seen for MYSQL connections; however, instead of MYSQL it looks like an attempt at Access instead.

surendraringwal: As Rabbit stated: We will need to have the EXACT Error message. Include the Title, Number, and Text exactly as given in any error message that you are receiving - don't shorten or alter this information.

Also, YOU need to perform the basic trouble shooting steps to locate which line the error has occured on. We do not have hands-on to your computer nor the database you are attempting to make a connection.
Oct 12 '13 #3

P: 3
this is java code using jdbc to insert values in three tables of a single database
Oct 12 '13 #4

P: 3
Error ocurred is as under after running the code

run:
SQL Exception:
State : 21S01
Message: [Microsoft][ODBC Microsoft Access Driver] Number of query values and destination fields are not the same.
Error : -3520
BUILD SUCCESSFUL (total time: 0 seconds)
Oct 12 '13 #5

Rabbit
Expert Mod 10K+
P: 12,421
Your thread has been moved to the java forum.

The error is pretty clear. The number you fields you're trying to insert do not match. Either you have more fields in the table than you do in your update or you have less.
Oct 12 '13 #6

Post your reply

Sign in to post your reply or Sign up for a free account.