Avatar of STEVE00098
STEVE00098
 asked on

java file that will expose two methods

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
JavaJSP

Avatar of undefined
Last Comment
STEVE00098

8/22/2022 - Mon
Mick Barry

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

for_yan

you need to import

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

ASKER
or how can i call a stored procedure from .java and have the output as a return value
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Mick Barry

> 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 Barry

STEVE00098

ASKER
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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Mick Barry

see my previous comment
for_yan


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);
STEVE00098

ASKER
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)
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Mick Barry

you need to include the jdbc drivers jars in your classpath
for_yan

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
STEVE00098

ASKER
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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
for_yan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
STEVE00098

ASKER
is that okay if i remove void main from both methods
for_yan

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
for_yan

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

Your help has saved me hundreds of hours of internet surfing.
fblack61
STEVE00098

ASKER
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";
 
   }


      }