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