Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i create this simple java project:
CODE OF SIMPLE JAVA PROJECT:

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

class test{

	public static void main(String[] args) throws SQLException {

	
//	public boolean getConnection() throws SQLException{
	
		String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
		
		String server = "localhost";
	    String database = "Greco";
	    String jdbcUrl = "jdbc:sqlserver://"+server+";database="+database+";integratedSecurity=true";
  
	    	try   
	        {     
	                Class.forName(driverName);               
	        }   
	        catch (ClassNotFoundException e1)  
	        {     
	            System.out.println("Class.forNameException; "+e1.getMessage());     
	        }     
	                 
	        Connection con = null;     
	              
	        try  
	        {     
	            // System.out.println("database connected");  
	            con = DriverManager.getConnection(jdbcUrl); 
	        //    System.out.println("database connected"); 
	        //    System.out.println("Connessione: "+con.toString()); 
	        }  
	        catch(SQLException e)  
	        {     
	        	 System.out.println("database NOT connected"); 
	            System.out.println(""+e.getMessage());     
	        }     
	      
	     Statement st = null;     
	     ResultSet rs = null;     
	               st = con.createStatement();     
	              // st.executeQuery("select * from dbo.Fermentazione"); 
	               st.executeQuery("SELECT Top 1 [idSerbatoio],[nomeVino],[dataInizioFermentazione],[dataFineFermentazione]"+
  "FROM [Greco].[dbo].[Fermentazione]"+
"where idSerbatoio = '1001'"+
"order by dataInizioFermentazione desc");
	               rs = st.getResultSet();     
	            
	             
	               while(rs.next())   	               {     
	            	   // JDBC columns starts at 1       
	            	   System.out.print(rs.getString(1)+" ");
	            	   System.out.print(rs.getString(2)+" ");
	            	   System.out.print(rs.getString(3)+" ");
	            	   System.out.print(rs.getString(4)+" ");
	            	  // System.out.print(rs.getString(5)+" ");
	            	//   System.out.print(rs.getString(6)+"\n");
	            	   
	     			}
	       
	             
	           rs.close();
	           st.close();
	           con.close();
	           
	}

}


Executing the main class, this code works fine. But I would create a Dynamic Web Project, so i create a Dynamic Web Project and i put this code in a method like this:
CODE OF DYNAMIC WEB PROJECT:

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


public class ConnectionClass {
	
	public String getConnection() throws SQLException{
	Boolean connectionResult = false;
	String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
	String server = "localhost";
    String database = "Greco";
    String jdbcUrl = "jdbc:sqlserver://"+server+";database="+database+";integratedSecurity=true";

    	try   
        {     
                Class.forName(driverName);               
        }   
        catch (ClassNotFoundException e1)  
        {     
            System.out.println("Class.forNameException; "+e1.getMessage());     
        }     
                 
        Connection con = null;     
              
        try  
        {      
            con = DriverManager.getConnection(jdbcUrl); 
            connectionResult=true;
        }  
        catch(SQLException e)  
        {     
        	 System.out.println("database NOT connected"); 
            System.out.println(""+e.getMessage());     
        }     
      
     Statement st = null;     
     ResultSet rs = null;     
               st = con.createStatement();     
              // st.executeQuery("select * from dbo.Fermentazione"); 
               st.executeQuery("SELECT Top 1 [idSerbatoio],[nomeVino],[dataInizioFermentazione],[dataFineFermentazione]"+
"FROM [Greco].[dbo].[Fermentazione]"+
"where idSerbatoio = '1001'"+
"order by dataInizioFermentazione desc");
               rs = st.getResultSet();     
            
             
               while(rs.next())   	               {     
            	   // JDBC columns starts at 1       
            	   System.out.print(rs.getString(1)+" ");
            	   System.out.print(rs.getString(2)+" ");
            	   System.out.print(rs.getString(3)+" ");
            	   System.out.print(rs.getString(4)+" ");
            	  // System.out.print(rs.getString(5)+" ");
            	//   System.out.print(rs.getString(6)+"\n");
            	   
     			}
       
             
           rs.close();
           st.close();
           con.close();
           if(connectionResult)  return "Connessione stabilita";
           else return "Connessione non avvenuta";
}
}

Now, how can I test this dynamic web project? How can I create a client?
Thanks
Posted
Updated 10-Jun-14 22:13pm
v2

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900