sush
sush

Reputation: 6077

Having basic issue with java on ms sql server connection

Need help with below code.

I am trying to query from same table and update a record.

import java.sql.* ;

class Connect
{
 public static void main( String args[] )
 {
  try
  {
      // Load the database driver
      Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver" ) ;

      // Get a connection to the database
      Connection conn = DriverManager.getConnection( "jdbc:sqlserver://localhost:1433;user=sa;password=root;databaseName=blueprint") ;

      // Print all warnings
      for( SQLWarning warn = conn.getWarnings(); warn != null; warn = warn.getNextWarning() )
      {
          System.out.println( "SQL Warning:" ) ;
          System.out.println( "State  : " + warn.getSQLState()  ) ;
          System.out.println( "Message: " + warn.getMessage()   ) ;
          System.out.println( "Error  : " + warn.getErrorCode() ) ;
      }

      // Prepare a statement
      String sql = "select DN, WD from table1 ";

      Statement cs = conn.createStatement();
      // Execute the query
      ResultSet rs = cs.executeQuery(sql) ;


      // Loop through the result set
      int i = 0;
      while( rs.next() ) 

         System.out.println( rs.getString("DN") + " " + rs.getString("WD") ) ;
         String dm = rs.getString("DN");  
         String wi = rs.getString("WD");
         iupdateQuery(i, dm, wi);
         i++;


      // Close the result set, statement and the connection
      rs.close() ;
      cs.close() ;
      conn.close() ;

  }
  catch( SQLException se )
  {
      System.out.println( "SQL Exception:" ) ;

      // Loop through the SQL Exceptions
      while( se != null )
      {
          System.out.println( "State  : " + se.getSQLState()  ) ;
          System.out.println( "Message: " + se.getMessage()   ) ;
          System.out.println( "Error  : " + se.getErrorCode() ) ;

          se = se.getNextException() ;
      }
  }
  catch( Exception e )
  {
      System.out.println( e ) ;
  }
 }

 private void iupdateQuery(int i, String dm, String wi) {

                String sdm = dm;
                String swi = within;

                String query = "UPDATE table1 SET WDIO ='" + swi
                                + "' WHERE (DN ='" + sdm + "');";

                // debug
                System.out.println(i + ". " + query);

                try {

                        Statement update = conn.createStatement();
                        update.executeUpdate(query);

                } catch (Exception e) {
                        // debug out output this way
                        System.err.println("Mysql Statement Error: " + query);
                        e.printStackTrace();
                }
        }

}

Getting below error if i execute.

C:\test>javac Connect.java
Connect.java:48: non-static method iupdateQuery(int,java.lang.String,java.lang
tring) cannot be referenced from a static context
         iupdateQuery(i, dm, within);
         ^
Connect.java:91: cannot find symbol
symbol  : variable conn
location: class Connect
                        Statement update = conn.createStatement();
                                           ^
2 errors

Please let me know if i am doing any thing wrong.

Upvotes: 0

Views: 518

Answers (3)

Hector Sanchez
Hector Sanchez

Reputation: 2317

Change the method to static , because you are calling it from a static method, in this case the main method.

private static void iupdateQuery(int i, String dm, String wi)

Also I see an error

You have

   while( rs.next() ) 

         System.out.println( rs.getString("DN") + " " + rs.getString("WD") ) ;
         String dm = rs.getString("DN");  
         String wi = rs.getString("WD");
         iupdateQuery(i, dm, wi);
         i++;

You are missing the {} if not you are just doin' the first line System.out.println( rs.getString("DN") + " " + rs.getString("WD") ) ;

It should be.

while( rs.next() ) 
    {
             System.out.println( rs.getString("DN") + " " + rs.getString("WD") ) ;
             String dm = rs.getString("DN");  
             String wi = rs.getString("WD");
             iupdateQuery(i, dm, wi);
             i++;
    }

Upvotes: 1

Dan W
Dan W

Reputation: 5782

Try changing the iupdateQuery method to static:

private static void iupdateQuery(int i, String dm, String wi) {

Since you are calling this method from main, and main is static, this method must also be static, unless you call this method from an object.

Upvotes: 3

Diego
Diego

Reputation: 18379

Make the method static, like this:

private static void iupdateQuery(int i, String dm, String wi) {
...

Upvotes: 4

Related Questions