目次
1. 最近何をしましたか。
5月上旬より、主に管理者側の機能改善と利用者側のバグ修正を行っています。
前作では、データベースの新規作成機能とデータテーブルの新規作成機能を実装した際、作成されたテーブルは実体を持たず、対応するシステムデータテーブルにテーブルのスキーマ情報を格納するだけで、実体を持たなかった。実際に MySQL で実際のデータベースを作成し、対応するデータベースに実際のテーブルを作成します。
そのため、システムのバックエンド方式を改善する必要があります。
システム フレームワークは、以前はデータベース処理に mybatis を直接使用していましたが、データ ソースが構成されており、新しく作成されたデータベースをプロジェクトに動的に構成することはできないため、プロジェクトはデータベース操作に JDBC を使用します。
私が担当する部分では、JdbcTemplate を使用しています。JdbcTemplate について簡単に紹介し、それを使用する際に遭遇したいくつかの問題と解決策について話しましょう。
2. はじめに、問題と解決策
1. JdbcTemplate とは
Spring フレームワークは JDBC をカプセル化し、JdbcTemplate を使用してデータベース操作を容易にします。
2. JdbcTemplate の準備
jar パッケージをインポートする
spring-beans-4.1.2.RELEASE.jar
spring-core-4.1.2.RELEASE.jar
spring-jdbc-4.1.2.RELEASE.jar
spring-tx-4.1.2.RELEASE.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
完全なxml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="jdbc:mysql:///user_db" /><!--对应SQLyog里的数据库-->
<property name="username" value="root" /> <!-- 用户名 -->
<property name="password" value="4.233928" /> <!-- 密码 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
</bean>
<!-- JdbcTemplate对象 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入dataSource属性-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 组件扫描 -->
<context:component-scan base-package="JDBC"></context:component-scan>
</beans>
3. JdbcTemplate の使用 - メソッドの実行と更新
JdbcTemplate には、SQL ステートメントを実行するための 3 種類のメソッドがあります。
execute
: すべての SQL ステートメントを実行できます。通常、DDL ステートメントの実行に使用されます。update
:INSERT
、UPDATE
などDELETE
の DML ステートメントを実行するために使用されます。query
SELECT
: このようなDQL データ クエリ ステートメントに使用されます。
私自身の使用では、主にexecuteメソッドとupdateメソッドを使用していました.executeメソッドを使用してデータベーステーブルを削除すると、エラーが発生することがわかりました.executeメソッドはパラメーターを渡す方法がないため、方法がありませんでした.フロントエンドが通過したテーブル名を削除します。
4. 実行方法と更新方法の違い (参考)
- update はパラメーターを取ることができますが、execute はできません。例えば:
jdbcTemplate.update("update TableA set name = 'Andy’ where id=?", new Object[] {new Integer(3)});
jdbcTemplate.update("insert into sys_person(person_id, person_name, gender) values(?,?,?)",new Object[]{"eeee","eeee","eeee"});
jdbcTemplate.execute("update TableA set name = 'Andy’ where id=3");
- 更新は java.sql.PreparedStatement を使用して行われ、実行は java.sql.Statement に基づいています。
- update は、影響を受ける行の数である int を返します。execute は void を返します
5. 最後に、execute がパラメーターを渡せないという問題を解決するにはどうすればよいですか?
JdbcTemplate の代わりに JDBC ステートメントを直接使用する
コードは以下のように表示されます:
public int specificDelete(int id,int DBId)throws Exception{
DB db=dbMapper.findDBById(DBId);
String dbName=db.getDBEN();
System.out.println(dbName);
Chart chart=chartMapper.findChartById(id,DBId);
String chartName= chart.getEN();
System.out.println(chartName);
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/"+dbName;
String user = "root";
String password = "xxxxx";
Connection connection=null;
try{
Class.forName(driver);
connection = DriverManager.getConnection(url, user, password);
String sql="drop table "+chartName;
PreparedStatement statement = connection.prepareStatement(sql);
int resultSet = statement.executeUpdate();
System.out.println(resultSet);
}catch (ClassNotFoundException e){
e.printStackTrace();
}catch (SQLException e){
e.printStackTrace();
}
connection.close();
return 1;
}