Spring jdbcTemplate

cpliu903 2009-07-27 03:44:52
Spring jdbcTemplate

org.springframework.jdbc.core.JdbcTemplate

怎样可以取的 table column name?
可否给例子?

...全文
74 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
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>
forandever 2009-07-27
  • 打赏
  • 举报
回复
用这个就是使用的原生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'

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧