JavaBean database operation package

Source: Internet
Author: User

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 ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.