Some basic steps for database programming:
Registration driver
Create a database connection
Execute SQL statement get the result
Some basic classes to use
CONNECTION Class
Statement class and PeparedStatement classes
Resultset class
Need to pay attention
When the database operation, it will appear, pay attention to capture and process.
Use the following code to explain the driver of the database (hereinafter given below is to connect the ODBC database)
String striver = "sun.jdbc.odbc.jdbcodbcdriver"; // Define the URL of the data (name)
String strConnection = "jdbc: odbc: myFirstData"; // user name data access String strConnectUsername = ""; // password for database access String strConnectPassword = ""; Connection conn = null; Statement stmt = null; ResultSet rs = null String SQL_TEMP = "Select * from tablename" try {
Class.forName (strDriver); // Register drive} catch (ClassNotFoundException ex1) {ex1.printStackTrace (); // capture driver registration process anomalies obtained} try {// database connection conn = DriverManager.getConnection (strConnection, strConnectUsername , strConnectPassword); stmt = conn.CreateStatement (); // create a Statement object stmtrs = stmt.executeQuery (sql_temp); // stmt using SQL operations to return a result set rs} catch (SQLException ex2) {ex2.printStackTrace ();
Come an example to explain how? Function Description Write a user login program, the user enters the password, click the "Login" button, find it in the user data sheet, if there is a corresponding record, tell: Hello, welcome you! If there is no user, it will be prompted: the username or password is incorrect, please re-login. Wow, I finally went to write a little person to touch the dog-like program. I used to call the procedure before, it is simply the name of the three-structured structure to deceive people. . .
Create a database: In order to simply use Access to build one, the name of the database is User, set it in ODBC. What, don't know how to set it? Oh, I really don't have a mood to explain with you. Please solve this problem and look down.
Create a data table: useertable
User number Userno text
User password Password text
The department number DEPARTNO text
Fill in a few values
111 111 001
222 222 002
333 333 003 Landing JSP (I am afraid I will tell you how to write such JSP)
Login.jsp
<% @ Page ContentType = "text / html; charset = GB2312"%>
<% @ Taglib Uri = "/ Web-INF / STRUTS-TILES.TLD" prefix = "tiles"%>
<% @ Taglib URI = "/ Web-INF / STRUTS-HTML.TLD" prefix = "html"%>
<% @ Taglib URI = "/ Web-INF / STRUTS-LOGIC.TLD" prefix = "logic"%>
<% @ Taglib Uri = "/ Web-INF / STRUTS-Bean.tld" prefix = "bean"%>
Please enter h3> User Name:
Password:
Page Success.jsp displaying the correct result
<% @ page contenttype = "text / html; charset = GB2312"%>
Error.jsp
<% @ page contenttype = "text / html; charset = GB2312"%>
Ok, let's go to the programmer to do the job, that is, write the Java code.
First, don't ask where to write Java code
Second, don't ask what way to compile the Java code into class files.
Third, don't ask where Java files and class files are placed
... I won't answer any more such a problem.
Don't forget to import two JAR packages.
FORM used to save data
Userform.java
Import org.apache.struts.action. *;
Public Class Userform Extends ActionForm
{
PRIVATE STRING UserId = NULL;
Private string password = null;
Public string getUserid () {
RETURN UserID == NULL? ": UserId.trim ();
Public void setUserid (String UserID) {
this. Userid = UserId;
}
Public string getpassword () {
RETURN Password == NULL? ": Password.trim ();
}
Public void setpassword (string str2) {
THIS. Password = password;
}
}
I saw FORM, there is nothing, is some attributes and GET and SET methods about attributes.
Action used to process data
UserAction.java
Import javax.servlet.http.httpservletRequest;
Import javax.servlet.http.httpservletResponse;
Import org.apache.struts.Action.Actionerro;
Import org.apache.struts.Action.actionform;
Import org.apache.struts.Action.actionForward;
Import org.apache.struts.action.actionmapping;
Import org.apache.struts.Actions.dispatchaction;
Public Class UseerAction Extends Dispatchction
{
Public ActionForward Execute (ActionMapping Mapping,
Actionform Form,
HTTPSERVLETREQUEST REQUEST,
Httpservletresponse response
Throws Exception
{
// Get page form information
Userform uform = (userform) Form;
/ / Get two data already submitted from the FROM
String temp_id = uform. GetUserid ();
String Temp_pass = uform. Getpassword ();
IF (! "". Equals (TEMP_ID) &&! "" "Equals (TEMP_PASS))
{
/ / Define the driver of the database (hereinafter given below is to connect the ODBC database)
String striver = "sun.jdbc.odbc.jdbcodbcdriver";
/ / Define the URL of the database (name)
String strconnection = "JDBC: ODBC: User";
// Username
String strconnectusername = "";
// Database Access password
String strconnectpassword = ""
Connection conn = NULL;
Statement Stmt = NULL;
ResultSet RS = NULL;
String SQL_TEMP = "SELECT * from UserTable Where
Userno = '" TEMP_ID "' AND
Password = '" TEMP_PASS "' ";
Try
{
Class.Forname (strDriver); // Register Driver}
Catch (ClassNotFoundException EX1)
{// capture exceptions during the driver registration
EX1.PRINTSTACKTRACE ();
}
Try
{
Conn = // Get database connections
Drivermanager.getConnection (STRCONNECTION,
StrConnectusername,
StrConnectPassword;
// Create a StMT STMT
STMT = conn.createstatement ();
// Use STMT to perform SQL operation returns to RS
RS = stmt.executequery (SQL_TEMP);
Catch (SQLEXCEPTION EX2)
{
EX2.PRINTSTACKTRACE ();
}
INT TEMP_COUNT = 0;
// How many records have been queried from the database through this cycle
While (rs.next ())
{
TEMP_COUNT ;
}
// If the query has a record, it indicates that the username and password are correct.
IF (Temp_count> 0)
{
Return mapping.findforward ("ok");
}
Else // If the query is not recorded, indicate the username or password error
{
Return mapping.findforward ("ERR");
}
}
Else
{
Return mapping.findforward ("ERR");
}
}
}