Compile your own JDBC framework

Source: Internet
Author: User

 

Compile your own JDBC framework

The data used to define data is called metadata.

Metadata-databasemetadata

L metadata: Definition information of databases, tables, and columns.

L connection. getdatabasemetadata ()

L databasemetadata object

• Geturl (): returns a string class object, representing the URL of the database.

• GetUserName (): returns the username used to connect to the current database management system.

• Getdatabaseproductname (): returns the product name of the database.

• Getdatabaseproductversion (): returns the database version number.

• Getdrivername (): returns the name of the driver.

• Getdriverversion (): returns the driver version number.

• Isreadonly (): returns a Boolean value indicating whether the database only supports read operations.

Metadata-Parametermetadata

L preparedstatement. getparametermetadata ()

• Obtain the parametermetadata object representing the preparedstatement metadata.

• Select * from user where name =? And Password =?

L parametermetadata object

• Getparametercount ()

• Obtain the number of specified parameters

• Getparametertype (int param)

• Obtain the SQL type of the specified parameter

Metadata-ResultsetMetadata

L resultset. getmetadata ()

• Obtain the resultsetmetadata object that represents the metadata of the resultset object.

L resultsetmetadata object

• Getcolumncount()

• Returns the number of columns of the resultset object.

• Getcolumnname (INT column)

• Get the name of the specified Column

• Getcolumntypename (INT column)

• Obtain the type of the specified Column

Use metadata to simplify JDBC code

L business background: All entity objects in the system involve basic CRUD operations:

• The cud operation code of all entities is basically the same, and only the SQL statements sent to the database are different. Therefore, you can extract all the same code of the cud operation into an update method of the tool class, and define the SQL statement that the parameter receives the change.

• In addition to different SQL statements, the R operation of an object varies with the operation entity, and the mappings to the resultset are also different. Therefore, a query method can be defined, in addition to the SQL statements that receive changes in the form of parameters, the policy mode is used by the caller OF THE qurey method to determine how to map the data in the resultset to the object.

Userdaoimpl. Java

Public voidInsert ()ThrowsSqlexception {

/* ConnectionCon= NULL;

PreparedstatementSt= NULL;

ResultsetRS= NULL;

Con= Dbmanager. getconnection ();

StringSQL= "Insert into users (ID, name, password, email, birthday) values (?,?,?,?,?) ";

St= Con. preparestatement (SQL);

St. setint (1, 6 );

St. setstring (2 ,"Xxxx");

St. setstring (3, "1111 ");

St. setstring (4 ,"Aa@sina.com");

St. setdate (5, new java. SQL. Date (new date (). gettime ()));

St.exe cuteupdate (SQL);*/

String SQL = "insert into users (ID, name, password, email, birth ";

Object [] Params = {2, "XXX", "111", "xxx@126.com ",NewDate ()};

Dbmanager.Update(SQL, Params );

}

Public voidDelete ()ThrowsSqlexception {

/* ConnectionCon= NULL;

PreparedstatementSt= NULL;

ResultsetRS= NULL;

Con= Dbmanager. getconnection ();

StringSQL= "Delete from users where id =? ";

St= Con. preparestatement (SQL);

St. setint (1, 1 );

St.exe cuteupdate (SQL);*/

String SQL = "delete from users where id =? ";

Object [] Params = {4}; // delete user 4

Dbmanager.Update(SQL, Params );

}

Public voidUpdate ()ThrowsSqlexception {

/* ConnectionCon= NULL;

PreparedstatementSt= NULL;

ResultsetRS= NULL;

Con= Dbmanager. getconnection ();

StringSQL= "UpdateIsersSet Name = ?, Password =? Where id =? ";

St= Con. preparestatement (SQL);

St. setstring (1 ,"Xxx");

St. setstring (2, "111 ");

St. setint (3, 5 );

St.exe cuteupdate (SQL);*/

String SQL = "Update isers set name =? Password = ?, Where id =? ";

Object [] Params = {"www", "222", 1}; // update the name and password of user 1WWWAnd 222

Dbmanager.Update(SQL, Params );

}

Public voidFind ()ThrowsSqlexception {

Connection con =Null;

Preparedstatement ST =Null;

Resultset rs =Null;

Con = dbmanager.Getconnection();

String SQL = "select * from users where id =? ";

St = con. preparestatement (SQL );

St. setint (1, 6 );

Rs1_st.exe cutequery ();

While(Rs. Next ()){

// Rs an object Rs. getxxx (I)

}

If you write the following method in dbmanager. Java, you can directly call this method in the userdaoimpl. Java section.

Public static voidUpdate (string SQL, object [] Params ){

// Includes addition, deletion, modification, and other methods

Connection con =Null;

Preparedstatement ST =Null;

Resultset rs =Null;

Try{

Con = dbmanager.Getconnection();

St = con. preparestatement (SQL );

For(IntI = 0; I <Params. length; I ++ ){

St. setobject (I + 1, Params [I]);

}

St.exe cuteupdate (SQL );

}Catch(Sqlexception e ){

//TodoAuto-generated Catch Block

E. printstacktrace ();

}Finally{

Dbmanager.Release(Con, St, RS );

}

}

The red part is the code required to connect to the database before the update method is written in dbmanager. java.

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.