import java.awt.event.*;
import java.sql.*;
import java.util.Vector;
import sun.jdbc.odbc.JdbcOdbcDriver;
public class DatabaseUtilities
{
static String SQLString = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
static String dataName = "testData";
static String SQLurl = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName = ";
private ActionListener exceptionListener = null;
public DatabaseUtilities()
{
registerDriver();
}
public void registerDriver()
{
try
{
Class.forName(SQLString);
DriverManager.registerDriver(new JdbcOdbcDriver());

}
catch (ClassNotFoundException ex)
{
reportException(ex.getMessage());
}
catch(SQLException e)
{
reportException(e.getMessage());
}
}
public void execute(String SQLCommand)
{
String url = SQLurl +dataName;
try
{
Connection connection = DriverManager.getConnection(url,"sa","123");
Statement statement = connection.createStatement();
statement.execute(SQLCommand);
connection.close();
}
catch (SQLException e)
{
reportException(e.getMessage());
}
}
public void setExceptionListener(ActionListener exceptionListener)
{
this.exceptionListener = exceptionListener;
}
private void reportException(String exception)
{
if(exceptionListener!= null)
{
ActionEvent event = new ActionEvent(this,0,exception);
exceptionListener.actionPerformed(event);
}
else
{
System.err.println(exception);
}
}

}