Database Connection operations (1), database connection operations (

Source: Internet
Author: User

Database Connection operations (1), database connection operations (

Connect to the Database Using JSP, including data query, deletion, and modification. add data to the database. The database is MySQL.

The files include login. jsp, do_login.jsp (process logon information), index. jsp (display logon username, password, and edit and delete operations), edit. jsp, do_edit.jsp, and delete. jsp.

Logon page


Login. jsp

<% @ Page language = "java" pageEncoding = "UTF-8" %> Do_login.jsp

<% @ Page language = "java" import = "java. SQL. * "pageEncoding =" UTF-8 "%> 

Index. jsp

<% @ Page language = "java" import = "java. SQL. * "pageEncoding =" UTF-8 "%> Edit page

After editing


Edit. jsp

<% @ Page language = "java" import = "java. SQL. * "pageEncoding =" UTF-8 "%> Process Edit page

Do_edit.jsp

<% @ Page language = "java" import = "java. SQL. * "pageEncoding =" UTF-8 "%> <% @ page import =" java. util. * "%> Delete page

Delete. jsp

<% @ Page language = "java" import = "java. SQL. * "pageEncoding =" UTF-8 "%> Before compiling and running, import the MySQL driver package to WebRoot/WEB-INF/lib.


Basic database connection code

The following is a connection to sqlserver 2005 using the java language.

/**
* This class is used to obtain and close database connection objects.
* @ Author student
*
*/
Public class DBConnection {

Private static final String DRIVER_CLASS = "com. microsoft. sqlserver. jdbc. SQLServerDriver ";
Private static final String DATABASE_URL = "jdbc: sqlserver: // localhost: 1035; databaseName = books ";
Private static final String DATABASE_USER = "sa ";
Private static final String DATABASE_PASSWORD = "123456 ";

/**
* Obtain a data connection object
* @ Return
*/
Public Connection getConn (){
Connection con = null;
Try {
Class. forName (DRIVER_CLASS );
Con = DriverManager. getConnection (DATABASE_URL, DATABASE_USER, DATABASE_PASSWORD );
} Catch (ClassNotFoundException ex ){
Ex. printStackTrace ();
} Catch (SQLException ex ){
Ex. printStackTrace ();
}
Return con;
}

/**
* Shut down database-related objects
*/
Public void closeConn (ResultSet rs, PreparedStatement ps, Connection con ){
Try {
If (rs! = Null)
Rs. close ();
If (ps! = Null)
Ps. close ();
If (con! = Null)
Con. close ();
} Catch (SQLException ex ){
Ex. printStackTrace ();
}
}
}

The following is a connection to sqlserver 2000 using the java language.

Public class BaseJdbcDAO {
Protected Connection conn = null;
Protected Statement stmt = null;
Protected PreparedStatement pstmt = null;
Protected ResultSet rs = null;
Protected void openConn (){
Connection aConn = null;
Try {
Class. forName ("com. microsoft. jdbc. sqlserver. SQLServerDriver ");
AConn = DriverManager. getConnection ("jdbc: microsoft: sqlserver: // localhost: 1433; DataBaseName ...... full text >>>

What operations do I need to connect an application to a database?

It depends on the language you are using. Establish a connection, open the database, operate the database, and close the database. For more information, see Ado.net. If it is. net or jdbc, if it is java

Related Article

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.