Package dbconn;
Import java. SQL .*;
Public class dbresult
...{
Private connection con;
Statement stmt;
Resultset rs = NULL;
Public dbresult ()...{
Try ...{
This. Con = getconnection ();
} Catch (exception e )...{
// Todo automatically generates catch Blocks
E. printstacktrace ();
}
}
Public static connection getconnection () throws exception ...{
Connection con = NULL;
Try ...{
// Class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver ");
// Con = drivermanager. getconnection ("JDBC: ODBC: mycms", "sa ","");
Class. forname ("com. Microsoft. JDBC. sqlserver. sqlserverdriver ");
Con = drivermanager. getconnection ("JDBC: Microsoft: sqlserver: // localhost: 1433; databasename = db_shopping; user = sa; Password = ");
} Catch (classnotfoundexception e )...{
System. Out. println (E. getmessage ());
} Catch (sqlexception e )...{
System. Out. println (E. getmessage ());
}
If (con = NULL)
System. Out. println ("error ");
Return con;
}
/***//**
* Used to obtain the resultset object for executing SQL statements
*/
Public resultset getresult (string SQL )...{
Try ...{
Con = getconnection ();
Stmt = con. createstatement ();
Rs1_stmt.exe cutequery (SQL );
}
Catch (exception e )...{}
Return Rs;
}
/***//**
* No return value is returned for SQL statement execution.
*/
Public int executeupdate (string SQL) throws sqlexception ...{
Int result = 0;
Try ...{
Con = getconnection ();
Stmt = con. createstatement ();
Stmt.exe cuteupdate (SQL );
} Catch (exception e )...{
// Todo automatically generates catch Blocks
E. printstacktrace ();
}
Return result;
}
/***//**
* Used to obtain the preparedstatement (preprocessing) object for executing SQL statements
*/
Public preparedstatement getpreparedstatement (string SQL )...{
Try ...{
Con = getconnection ();
Preparedstatement pstmt = con. preparestatement (SQL );
Return pstmt;
}
Catch (exception e )...{}
Return NULL;
}
/***//**
* Close the connection.
*/
Public void closecon ()...{
Try ...{
This. Con. Close ();
} Catch (exception e )...{
E. printstacktrace ();
}
}
}