We help IT Professionals succeed at work.

java file that will expose two methods

STEVE00098
STEVE00098 asked
on
302 Views
Last Modified: 2012-05-11
I am tring to copile  the below java to a class file. But I get the error message ( all the way down) please help
public class Stringtoarray

{
      public static String[] toarray(String s)
      {
      String[] array =s.split(";");
      return array;
      }

 public static void main(String[] args) {

      // Create a variable for the connection string.
      String connectionUrl = "jdbc:sqlserver://sqlserver:1433;" +
         "databaseName=abm;user=janitorial;password=janitorial";

      // Declare the JDBC objects.
      Connection con = null;
      Statement stmt = null;
      ResultSet rs = null;

      try {
         // Establish the connection.
         Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
         con = DriverManager.getConnection(connectionUrl);

         // Create and execute an SQL statement that returns some data.
         String SQL = "SELECT TOP 10 * FROM Person.Contact";
         stmt = con.createStatement();
         rs = stmt.executeQuery(SQL);

         // Iterate through the data in the result set and display it.
         while (rs.next()) {
            System.out.println(rs.getString(4) + " " + rs.getString(6));
         }
      }

      // Handle any errors that may have occurred.
      catch (Exception e) {
         e.printStackTrace();
      }
      finally {
         if (rs != null) try { rs.close(); } catch(Exception e) {}
         if (stmt != null) try { stmt.close(); } catch(Exception e) {}
         if (con != null) try { con.close(); } catch(Exception e) {}
      }
   }


      }
Error
Stringtoarray.java:17: cannot find symbol
symbol  : class Connection
location: class Stringtoarray
      Connection con = null;
      ^
Stringtoarray.java:18: cannot find symbol
symbol  : class Statement
location: class Stringtoarray
      Statement stmt = null;
      ^
Stringtoarray.java:19: cannot find symbol
symbol  : class ResultSet
location: class Stringtoarray
      ResultSet rs = null;
      ^
Stringtoarray.java:24: cannot find symbol
symbol  : variable DriverManager
location: class Stringtoarray
         con = DriverManager.getConnection(connectionUrl);
               ^
4 errors
Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
you're missing imports

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Stringtoarray

{
	public static String[] toarray(String s) {
		String[] array = s.split(";");
		return array;
	}

	public static void main(String[] args) {

		// Create a variable for the connection string.
		String connectionUrl = "jdbc:sqlserver://sqlserver:1433;"
				+ "databaseName=abm;user=janitorial;password=janitorial";

		// Declare the JDBC objects.
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;

		try {
			// Establish the connection.
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			con = DriverManager.getConnection(connectionUrl);

			// Create and execute an SQL statement that returns some data.
			String SQL = "SELECT TOP 10 * FROM Person.Contact";
			stmt = con.createStatement();
			rs = stmt.executeQuery(SQL);

			// Iterate through the data in the result set and display it.
			while (rs.next()) {
				System.out.println(rs.getString(4) + " " + rs.getString(6));
			}
		}

		// Handle any errors that may have occurred.
		catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (rs != null)
				try {
					rs.close();
				} catch (Exception e) {
				}
			if (stmt != null)
				try {
					stmt.close();
				} catch (Exception e) {
				}
			if (con != null)
				try {
					con.close();
				} catch (Exception e) {
				}
		}
	}

}

Open in new window

Awarded 2011
Awarded 2011

Commented:
you need to import

import java.sql.Statement
import java.sql.Connection

Author

Commented:
or how can i call a stored procedure from .java and have the output as a return value
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> or how can i call a stored procedure from .java and have the output as a return value


basically the same as what you are doing
just use the code I posted above with the correct imports
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:

Author

Commented:
thank you objects and for_yan. After adding the import statement the java compiled successfully. how can iI call a stored procedure from java and the output of sp as a return value
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
see my previous comment
Awarded 2011
Awarded 2011

Commented:

http://docsrv.sco.com/JDK_guide/jdbc/getstart/callablestatement.doc.html

this is how to use CallableStatment with stored procedures, e.g.

  CallableStatement cstmt = con.prepareCall(
                          "{call getTestData(?, ?)}");
    cstmt.registerOutParameter(1, java.sql.Types.TINYINT);
    cstmt.registerOutParameter(2, java.sql.Types.DECIMAL, 3);
    cstmt.executeQuery();
    byte x = cstmt.getByte(1);
    java.math.BigDecimal n = cstmt.getBigDecimal(2, 3);

Author

Commented:
Thank you for the prompt reply
I was able to successfullly compile the java but when i run the java

D:\WebSphere\java\bin>java Stringtoarray
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
        at java.lang.Class.forName(Class.java:136)
        at Stringtoarray.main(Stringtoarray.java:29)
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
you need to include the jdbc drivers jars in your classpath
Awarded 2011
Awarded 2011

Commented:
I guess, they should be downloaded from here, unless you already have the jar which you
need to include in yourr classpath
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=a737000d-68d0-4531-b65d-da0f2a735707&displaylang=en

Author

Commented:
You helped me with this issue couple of months ago. I want to add one more method on the below class. Technically when other application calls stringtoarray there should be two methods available one for converting string to stringtoarray and the other calling the sql statement.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;


public class Stringtoarray

{
      public static String[] toarray(String s)
      {
      String[] array =s.split(";");
      return array;
      }


public static void main (String [] args) {
String [] s0 = toArray(args[0]);
for(int j=0; j<s0.length; j++){System.out.println(s0[j]);
}

public static void main(String[] args) {

      // Create a variable for the connection string.
      String connectionUrl = "jdbc:sqlserver://abm-sql1\\abm-jan-sql1:1433;" +
         "databaseName=jani;user=sa;password=testing;

      // Declare the JDBC objects.
      Connection con = null;
      Statement stmt = null;
      ResultSet rs = null;

      try {
         // Establish the connection.
         Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
         con = DriverManager.getConnection(connectionUrl);

         // Create and execute an SQL statement that returns some data.
         String SQL = "SELECT TOP 10 *  FROM billing.tbl_Expenses";
         stmt = con.createStatement();
         rs = stmt.executeQuery(SQL);

         // Iterate through the data in the result set and display it.
         while (rs.next()) {
            System.out.println(rs.getString(4) + " " + rs.getString(6));
         }
      }

      // Handle any errors that may have occurred.
      catch (Exception e) {
         e.printStackTrace();
      }
      finally {
         if (rs != null) try { rs.close(); } catch(Exception e) {}
         if (stmt != null) try { stmt.close(); } catch(Exception e) {}
         if (con != null) try { con.close(); } catch(Exception e) {}
      }
   }


 

      }
when i compile the above program i get the below message
D:\WebSphere\java\bin>javac Stringtoarray.java
Stringtoarray.java:22: illegal start of expression
public static void main(String[] args) {
^
Stringtoarray.java:22: illegal start of expression
public static void main(String[] args) {
       ^
Stringtoarray.java:22: ';' expected
public static void main(String[] args) {
             ^
Stringtoarray.java:22: '.class' expected
public static void main(String[] args) {
                                 ^
Stringtoarray.java:22: ';' expected
public static void main(String[] args) {
                                     ^
Stringtoarray.java:63: reached end of file while parsing
        }¿
         ^
6 errors

thank you
Awarded 2011
Awarded 2011
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
is that okay if i remove void main from both methods
Awarded 2011
Awarded 2011

Commented:
No, it is not.

Your class should have one method
with this signature:

public static void main(String [] args)

This would be the method where execution will
start. So it should have one such method, if you will
start execution with this class.
But there should not be two of them
Awarded 2011
Awarded 2011

Commented:
I corrected a couple of misplaced braces and lost closing quote

Still uyou have two main() methods and
there is no such method toArray()

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;


public class Stringtoarray

{
      public static String[] toarray(String s)
      {
      String[] array =s.split(";");
      return array;
      }


public static void main (String [] args) {
String [] s0 = toArray(args[0]);         // No method toArray
for(int j=0; j<s0.length; j++){System.out.println(s0[j]);
}
}

public static void main(String[] args) {                     //second time main

      // Create a variable for the connection string.
      String connectionUrl = "jdbc:sqlserver://abm-sql1\\abm-jan-sql1:1433;" +
         "databaseName=jani;user=sa;password=testing";

      // Declare the JDBC objects.
      Connection con = null;
      Statement stmt = null;
      ResultSet rs = null;

      try {
         // Establish the connection.
         Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
         con = DriverManager.getConnection(connectionUrl);

         // Create and execute an SQL statement that returns some data.
         String SQL = "SELECT TOP 10 *  FROM billing.tbl_Expenses";
         stmt = con.createStatement();
         rs = stmt.executeQuery(SQL);

         // Iterate through the data in the result set and display it.
         while (rs.next()) {
            System.out.println(rs.getString(4) + " " + rs.getString(6));
         }
      }

      // Handle any errors that may have occurred.
      catch (Exception e) {
         e.printStackTrace();
      }
      finally {
         if (rs != null) try { rs.close(); } catch(Exception e) {}
         if (stmt != null) try { stmt.close(); } catch(Exception e) {}
         if (con != null) try { con.close(); } catch(Exception e) {}
      }
   }

}

Open in new window

Author

Commented:
the below program compiled successfully. how can i test it  from command propmt
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;


public class Stringtoarray

{
      public  String[] toarray(String s)
      {
      String[] array =s.split(";");
      return array;
      }


public String calldb() {

   // Create a variable for the connection string.
      String connectionUrl = "jdbc:sqlserver://abm-sql1\\abm-jan-sql1:1433;" +
         "databaseName=jani;user=sa;password=testing;

      // Declare the JDBC objects.
      Connection con = null;
      Statement stmt = null;
      ResultSet rs = null;

      try {
         // Establish the connection.
         Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
         con = DriverManager.getConnection(connectionUrl);

         // Create and execute an SQL statement that returns some data.
         String SQL = "SELECT TOP 10 *  FROM billing.tbl_Expenses";
         stmt = con.createStatement();
         rs = stmt.executeQuery(SQL);

         // Iterate through the data in the result set and display it.
         while (rs.next()) {
            System.out.println(rs.getString(4) + " " + rs.getString(6));
         }
      }

      // Handle any errors that may have occurred.
      catch (Exception e) {
         e.printStackTrace();
      }
      finally {
         if (rs != null) try { rs.close(); } catch(Exception e) {}
         if (stmt != null) try { stmt.close(); } catch(Exception e) {}
         if (con != null) try { con.close(); } catch(Exception e) {}
      }
  return "test";
 
   }


      }

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.