数据库技术-JDBC中的PreparedStatement和Transaction PreparedStatement 首先介绍PreparedStatement: 1、PreparedStatement是一种Statement 2、比父接口提供了更多可以让我们用的方式. prepstmt = conn.prepareStatement(INSERT INTO animal VALUES(?,?,?))
数据库技术-JDBC中的PreparedStatement和Transaction
PreparedStatement首先介绍PreparedStatement: 1、PreparedStatement是一种Statement 2、比父接口提供了更多可以让我们用的方式. prepstmt = conn.prepareStatement("INSERT INTO animal VALUES(?,?,?)"); prepstmt.setInt(1, 1);
prepstmt.setString(2, "pig");
prepstmt.setInt(3, 10);
prepstmt.execute();
三个? 表示三个占位符,设定三个位置的值。 1表示第一个问号的值,2表示第二个问号的值,3表示第三个问号的值。
可以这样理解: 我首先准备好一条sql语句,这条语句中有三个值等待确定,接下来依次确定三个值的类型和值。
package myjdbc; import java.sql.*; public class PreparedJdbc { private static String driver = "com.mysql.jdbc.Driver"; private static String url = "jdbc:mysql://localhost:3308/zoo"; private static String user = "root"; private static String password = "123"; private static Connection conn = null; private static PreparedStatement prepstmt = null; private static ResultSet rs = null; public static void main(String[] args) { try { Class.forName(driver); // conn = DriverManager.getConnection(url,user,password); prepstmt = conn .prepareStatement("INSERT INTO animal VALUES(?,?,?)"); prepstmt.setInt(1, 1); prepstmt.setString(2, "pig"); prepstmt.setInt(3, 10); prepstmt.execute(); System.out.println("finish!"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); } if (prepstmt != null) { prepstmt.close(); } if (conn != null) { conn.close(); } } catch (Exception e2) { // TODO: handle exception } } } }
MySQL:

Transaction
先举个简单的例子,A账户转账到B账户,一般需要两条sql语句,一条update A账户上的钱,一条update B账户的钱。这两天update语句必须同时执行成功,或者同时不执行成功,不予许出现中间情况,一条成功一条不成功。
这两条语句就构成了transaction。
conn.setAutoCommit(false);//首先设置自动提交false
stmt = conn.createStatement();
stmt.addBatch("insert into animal values (51, '500', 3)");
stmt.addBatch("insert into animal values (52, '500', 4)");
stmt.addBatch("insert into animal values (53, '500', 5)");
stmt.executeBatch();//三条语句批处理
conn.commit();
conn.setAutoCommit(true);//还原默认自动提交true
设置回滚
catch (SQLException e) {
e.printStackTrace();
try {
if (conn != null) {
conn.rollback();
conn.setAutoCommit(true);
}
}
下面代码:
package myjdbc; import java.sql.*; public class TestTransaction { public static void main(String[] args) { Connection conn = null; Statement stmt = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection( "jdbc:mysql://localhost:3308/zoo", "root", "123"); conn.setAutoCommit(false); stmt = conn.createStatement(); stmt.addBatch("insert into animal values (51, '500', 3)"); stmt.addBatch("insert into animal values (52, '500', 4)"); stmt.addBatch("insert into animal values (53, '500', 5)"); stmt.executeBatch(); conn.commit(); conn.setAutoCommit(true); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); try { if (conn != null) { conn.rollback(); conn.setAutoCommit(true); } } catch (SQLException e1) { e1.printStackTrace(); } } finally { try { if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
Mysql:
Words in the end
在JDBC应用中,如果你已经是稍有水平开发者,你就应该始终以PreparedStatement代替Statement.也就是说,在任何时候都不要使用Statement.
基于以下的原因:
一.代码的可读性和可维护性.
虽然用PreparedStatement来代替Statement会使代码多出几行,但这样的代码无论从可读性还是可维护性上来说.都比直接用Statement的代码高很多档次:
stmt.executeUpdate("insert into tb_name (col1,col2,col2,col4) values ('"+var1+"','"+var2+"',"+var3+",'"+var4+"')");
perstmt = con.prepareStatement("insert into tb_name (col1,col2,col2,col4) values (?,?,?,?)");
perstmt.setString(1,var1);
perstmt.setString(2,var2);
perstmt.setString(3,var3);
perstmt.setString(4,var4);
perstmt.executeUpdate();
二.PreparedStatement尽最大可能提高性能.
每一种数据库都会尽最大努力对预编译语句提供最大的性能优化.因为预编译语句有可能被重复调用.所以语句在被DB的编译器编译后的执行代码被缓存下来,那么下次调用时只要是相同的预编译语句就不需要编译,只要将参数直接传入编译过的语句执行代码中(相当于一个涵数)就会得到执行.这并不是说只有一个Connection中多次执行的预编译语句被缓存,而是对于整个DB中,只要预编译的语句语法和缓存中匹配.那么在任何时候就可以不需要再次编译而可以直接执行.而statement的语句中,即使是相同一操作,而由于每次操作的数据不同所以使整个语句相匹配的机会极小,几乎不太可能匹配.比如:
insert into tb_name (col1,col2) values ('11','22');
insert into tb_name (col1,col2) values ('11','23');
即使是相同操作但因为数据内容不一样,所以整个个语句本身不能匹配,没有缓存语句的意义.事实是没有数据库会对普通语句编译后的执行代码缓存.
三.极大地提高了安全性.
Transaction 用在需要同步数据时非常重要,确保在几条批处理语句的同时执行成功。
2014.1.9

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use

Atom editor mac version download
The most popular open source editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)