JavaBean Operating Database

xiaoxiao2021-03-06  66

How do you usually use the JSP Operating Database? For JSP JavaBean mode, you must be very familiar with everyone, we can get database connections, queries, updates even package other features into javabean ----

Ok - let us get a problem with let us figure out: ** How do you get DB in the JSP page? Return to ResultSet from JavaBean, then enumerate in JSP? If this is the case, then I strongly recommend that you read this article. * ^ _ ^ *

Who will use the JavaBean package database? - Yes, everyone will, but - build a highly scalable "structure"? This is to use Java's related knowledge. Noteping less, let's create a DataSource-JDBC / Panabia in Tomcat, then create a Java "base class", this class encapsulates the release of database connection and connection: [There is corresponding annotation in the program]

CODE:

-------------------------------------------------- ------------------------------

Package panabia.db;

Import javax.sql.datasource;

Import javax.naming. *;

Import java.sql. *;

Public Class Sqlfactory

{

PRIVATE Static DataSource DS = NULL;

Private static object lock = new object ();

/ / Generate DataSource **

Public static datasource gainDataSource () {

Try {

IF (DS == NULL) {

Synchronized (LOCK) {

IF (DS == NULL) {

Context ctx = new initialContext ();

DS = (Datasource) CTX.lookup ("Java: Comp / Env / JDBC / Panabia");

}

}

}

}

Catch (namingexception e) {E.PrintStackTrace ();

Return DS;

}

/ / Generate SQL connection **

Public static synchronized connection gainConnection () {

Connection con = NULL;

Try {

IF (DS == NULL) {

GainDataSource ();

}

CON = ds.getConnection ();

}

Catch (Sqlexception E) {E.PrintStackTrace ();

Return con;

}

/ / Release SQL connection **

Public Static Void ReleaseConnection (ResultSet RS, PreparedStatement PS, Statement SQL, Connection Con) {

Try {

IF (rs! = null)

Rs.close ();

}

Catch (Sqlexception E) {E.PrintStackTrace ();

Try {

IF (PS! = NULL)

ps.close ();

}

Catch (Sqlexception E) {E.PrintStackTrace ();

Try {

IF (SQL! = NULL)

Sql.close ();

}

Catch (Sqlexception E) {E.PrintStackTrace ();

Try {

IF (con! = null &&! con?isclosed ()) con.close ();

}

Catch (Sqlexception E) {E.PrintStackTrace ();

}

}

-------------------------------------------------- ------------------------------

Everyone should notice that all the methods of this class is all static, which is mainly to facilitate other "extended classes" calls, of course, there are other benefits --- :)

Ok, this class has been packaged. Now we can write JavaBean separately for different application requirements, such as a simple: list all usernames and passwords in the Verify table in JSP -

How to do? - Generate Connection using SQLFactory, regenerates Statement, reproduce ResultSet - then enumerate? It seems good, oh, wait ... Do you have a feeling of "very kind"? --- Right, ASP, PHP is so - FAINT ~ How we returned to "primitive society" ....

Is there a better way? The answer is yes, Java's ability is the power of "Tongtian", as long as you can want to get, look at its API Document, it is not difficult to find a solution.

The answer came:

We return to Iterator to JSP enumeration in the query class, not the resultset.

Ok, our UserQuery class will produce:

CODE:

-------------------------------------------------- ------------------------------

Package panabia.opia;

Import panabia.db.sqlfactory;

Import java.util. *;

Import java.sql. *;

Public class userQuery {

Private arraylist list = null;

Private connection con = NULL;

Private statement SQL = NULL;

Private resultset = null;

Public iterator getResult () {

Try {

Con = SQLFActory.gainConnection ();

SQL = con.createstatement ();

RS = SQL.ExecuteQuery ("SELECT * from Verify");

// Verify table has only two fields: username, password;

List = new arraylist ();

While (rs.next ()) {

List.add (rs.getstring (1));

List.add (rs.getstring (2));

}

}

Catch (Sqlexception E) {E.PrintStackTrace ();

Finally {SQLFactory.ReleaseConnection (RS, NULL, SQL, CON);

Return list.iterator ();

}

}

-------------------------------------------------- ------------------------------

Then, the data is performed in the JSP page: because the cnjbb does not support the display of HTML tags, only the full Java codes in JSP --......

Iterator it = userQuery.getResult ();

While (it.hasnext ()) {

Out.print (String) it.next ());

}

........

It is so simple, a loop is getting it.

I admit that this is "indecent" "naked", "I am not yet," I want to beautify it. --- If you will, then began to do; if you feel that you can't do it, you need to find a web art to help you; OK - Obviously, this is much better than the case in JSP, and will not "move the whole body".

Not perfect: Although the situation is good, when using ArrayList to take out a large amount of data, the system resource will be considerably, the system will open a spatial storage result in the memory] --- related optimization method is , I don't make a statement here, you can refer to other relevant information.

转载请注明原文地址:https://www.9cbs.com/read-120051.html

New Post(0)