Spring jdbcTemplate Spring jdbcTemplateorg.springframework.jdbc.core.JdbcTemplate怎样可以取的 table column name?可否给例子? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用这个就是使用的原生SQL 查询了那么获取table Name,table comment,column Name, column comment等等信息,主要是依赖于你的SQL语句去数据库中查询。SQL语句有了之后,由JdbcTemplate 去执行查询,然后根据获取结果去进行你需要的操作。对应不同的数据库,需要的查询语句是不一样的,给你个查询的MySQL例子,其他的你自己去查吧:查询 columns :select * from information_schema.columns where table_schema = ? and table_name = ?查询 tables :select * from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA = ? and TABLE_TYPE = 'BASE TABLE' import javax.sql.DataSource; import java.sql.*; import org.springframework.jdbc.core.JdbcTemplate; public class UserDAO { private DataSource dataSource; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } public void insertUser(User user) { Connection conn = null; Statement stmt = null; try { conn = dataSource.getConnection(); stmt = conn.createStatement(); stmt.executeUpdate("INSERT INTO USER VALUES('"+ user.getId() + "', '" + user.getName() + "', '" + user.getSex() + "', '" + user.getAge() + "')"); } catch(SQLException e) { e.printStackTrace(); } finally { if(stmt != null) { try { stmt.close(); } catch(SQLException e) { e.printStackTrace(); } } if(conn != null) { try { conn.close(); } catch(SQLException e) { e.printStackTrace(); } } } } }2<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/TestDB</value> </property> <property name="username"> <value>caterpillar</value> </property> <property name="password"> <value>123456</value> </property> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"> <ref bean="dataSource"/> </property> </bean> <bean id="userDAO" class="onlyfun.caterpillar.UserDAO"> <property name="jdbcTemplate"> <ref bean="jdbcTemplate"/> </property> </bean> </beans> java算法征方案 RandomAccessFile能否读写序列化类 swing 线程安全 疑惑 JTextArea的getText()方法为什么得不到,文本框中的内容?是不是必须用setText()才行? 帮忙整整,实在不知道怎么整了,关于JavaScript和数据库的问题进行联动的 关于urlconnection.connection的问题 如何调用其他类中的方法 想要电子书网站,不妨看看 不惜分数,只求高手指点!泣血跪求 弱弱的求助--98下的JDK 1.4.1,怎样安装,设置路径,环境变量等.. Java数据类型 jnative调用C库回调函数的一个问题
给你个查询的MySQL例子,其他的你自己去查吧:查询 columns :
select * from information_schema.columns where table_schema = ? and table_name = ?查询 tables :
select * from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA = ? and TABLE_TYPE = 'BASE TABLE'
import java.sql.*;
import org.springframework.jdbc.core.JdbcTemplate; public class UserDAO {
private DataSource dataSource; public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void insertUser(User user) {
Connection conn = null;
Statement stmt = null;
try {
conn = dataSource.getConnection();
stmt = conn.createStatement();
stmt.executeUpdate("INSERT INTO USER VALUES('"+ user.getId() + "', '"
+ user.getName() + "', '"
+ user.getSex() + "', '"
+ user.getAge() + "')");
}
catch(SQLException e) {
e.printStackTrace();
}
finally {
if(stmt != null) {
try {
stmt.close();
}
catch(SQLException e) {
e.printStackTrace();
}
}
if(conn != null) {
try {
conn.close();
}
catch(SQLException e) {
e.printStackTrace();
}
}
}
}
}
2
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/TestDB</value>
</property>
<property name="username">
<value>caterpillar</value>
</property>
<property name="password">
<value>123456</value>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="userDAO" class="onlyfun.caterpillar.UserDAO">
<property name="jdbcTemplate">
<ref bean="jdbcTemplate"/>
</property>
</bean>
</beans>