Hi, i create this simple java project:
CODE OF SIMPLE JAVA PROJECT:
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 {
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);
}
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 Top 1 [idSerbatoio],[nomeVino],[dataInizioFermentazione],[dataFineFermentazione]"+
"FROM [Greco].[dbo].[Fermentazione]"+
"where idSerbatoio = '1001'"+
"order by dataInizioFermentazione desc");
rs = st.getResultSet();
while(rs.next()) {
System.out.print(rs.getString(1)+" ");
System.out.print(rs.getString(2)+" ");
System.out.print(rs.getString(3)+" ");
System.out.print(rs.getString(4)+" ");
}
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:
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 Top 1 [idSerbatoio],[nomeVino],[dataInizioFermentazione],[dataFineFermentazione]"+
"FROM [Greco].[dbo].[Fermentazione]"+
"where idSerbatoio = '1001'"+
"order by dataInizioFermentazione desc");
rs = st.getResultSet();
while(rs.next()) {
System.out.print(rs.getString(1)+" ");
System.out.print(rs.getString(2)+" ");
System.out.print(rs.getString(3)+" ");
System.out.print(rs.getString(4)+" ");
}
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