"MyBatis" 1:JDBC programming problems

Source: Internet
Author: User

Do Java development, how to operate the database. As you know, using JDBC (Java Data Base Connectivity), which is a Java API for executing SQL statements, provides unified access to a variety of relational databases, consisting of a set of classes and interfaces written in Java. JDBC provides a benchmark based on which we can build more advanced tools and interfaces that make it easier and easier for database developers to write database applications.

JDBC Programming 8-Step walk:

1 Loading the database driver

2 Creating and getting a database connection

3 Creating a JDBC Statement object

4 Setting Up SQL statements

5 setting parameters in SQL statements (using PreparedStatement)

6 Execute SQL statements and get results with statement

7 parsing the results of SQL execution

8 Releasing all resources (ResultSet, PreparedStatement, connection)

Smoothed out the coding idea, to, on the machine knock code ...

public class Jdbctest {/** * @param args */public static void main (string[] args) {//database connection Connection Connec
		tion = null;
		Precompiled statement, using pre-compiled statement to improve database performance PreparedStatement PreparedStatement = null;
		
		Result set ResultSet ResultSet = null;
			
			try {//Load Database driver Class.forName ("Com.mysql.jdbc.Driver"); Get the database link through the drive management class connection = Drivermanager.getconnection ("jdbc:mysql://localhost:3306/shop?characterencoding=
			Utf-8 "," Root "," ");
			Define SQL statement? represents a placeholder String sql = "SELECT * from user where username =?";
			Get preprocessing statement preparedstatement = connection.preparestatement (sql);
			Set the parameter, the first parameter is the ordinal of the parameter in the SQL statement (starting at 1), the second parameter is the parameter value set preparedstatement.setstring (1, "LJW");
			Issuing SQL execution queries to the database, querying the result set ResultSet = Preparedstatement.executequery (); Traverse the query result set while (Resultset.next ()) {System.out.println (resultset.getstring ("UID") + "" +resultset.getstring ("Usernam
			E "));
		}} catch (Exception e) {e.printstacktrace ();
		}finally{//release of all resources	if (resultset!=null) {try {resultset.close ();
				} catch (SQLException e) {e.printstacktrace ();
				}} if (Preparedstatement!=null) {try {preparedstatement.close ();
				} catch (SQLException e) {e.printstacktrace ();
				}} if (Connection!=null) {try {connection.close ();
				} catch (SQLException e) {e.printstacktrace (); }
			}

		}

	}

}

After writing the code, we go back to analyzing what's wrong with JDBC programming.

1 If you use JDBC for development, then the database connection will be created a lot, and each time the creation of the final release of resources, so frequent operations will seriously affect the performance of the system, but also cause a waste of system resources, a good solution is to use the database connection pool.

2 using JDBC for development, SQL statements are written dead in the code, that is, the so-called hard coding, which will make the code extremely difficult to maintain, because the actual requirements will be changed, once the requirements change, we will have to modify the Java source code, and to compile and deploy again, it is simply a hassle.

3 When using a PreparedStatement object to pass parameters to a placeholder in a SQL statement, there are also hard-coded cases where the where condition of the SQL statement in the actual application is not fixed, may or may be less, and each change will have to modify the Java code and the system maintenance is difficult.

4 in the final parsing and output of the result set, there are hard-coded cases (that is, query column name), so that, if the SQL statement changes, then the corresponding parsing code will also change, the system is not easy to maintain, a better solution is to encapsulate the database records into Pojo object and then parse. In summary, there are a lot of problems with JDBC programming, of course, the above list is only part of it, there are development efficiency issues, code reuse rate problem and so on. Understand these problems, we can understand better, then we have to learn how to solve these problems mybatis.

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.