jdbcTemplate获取数据表结构
jdbcTemplate 操作⽅法
Java代码
1. /**
2. *1.⽅法⼀:
3. */
4. String sql = "select * from "+ tableName;
5. //RowCountCallbackHandler rcch = new RowCountCallbackHandler();
6. //this.jdbcTemplateDao.query(sql, rcch);
7. //String[] coloumnName = ColumnNames();
8. //int[] coloumnType = ColumnTypes();
9. SqlRowSet sqlRowSet = this.jdbcTemplateDao.queryForRowSet(sql);
10. SqlRowSetMetaData sqlRsmd = MetaData();
11. int columnCount = ColumnCount();
12. for (int i = 1; i <= columnCount; i++) {
13. Map<String,String> fieldMap = new HashMap<String,String>();
14. fieldMap.put("name", ColumnName(i));
15. fieldMap.put("fieldType", String.ColumnType(i)));
16. tableFieldList.add(fieldMap);
17. }
18.
19. /**
20. *⽅法⼆:
21. */
22. String sql = "select * from "+ tableName;
23. RowCountCallbackHandler rcch = new RowCountCallbackHandler();
24. this.jdbcTemplateDao.query(sql, rcch);
25. String[] coloumnName = ColumnNames();
26. int[] coloumnType = ColumnTypes();
下⾯这⾥是JDBC的操作⽅法
Java代码
1. import java.sql.DriverManager;
2. import java.sql.ResultSet;
3. import java.sql.SQLException;
4. import java.sql.Statement;
5. import java.util.ArrayList;
6. import java.util.Iterator;
7. import java.util.List;
8.
9. sql.jdbc.Connection;
10. sql.jdbc.ResultSetMetaData;
11.
12. public class OperateDB {
13.
14. public static void main(String[] args) {
15. try {
16. Class.forName("sql.jdbc.Driver");
17. String url = "jdbc:mysql://192.168.11.211/education?useUnicode=true&characterEncod
ing=utf-8";
18. String user = "root";
19. String password = "12345678";
20. Connection con = (Connection) Connection(url, user, password);
21. Statement statement = ateStatement();
jdbctemplate查询一条数据22. ResultSet result = uteQuery("select * from t_sys_user");
23. 注意,这⾥,完全可以操作视图,操作表的sql语句与操作视图的sql语句⼀样,
24.
25.
26.
27.
28. ResultSetMetaData metadata = (ResultSetMetaData) MetaData();
29. List<String> metadataList = new ArrayList<String>();
30. System.out.println("====================表结构===========================
==");
31. for(int i = 1; i <= ColumnCount();i++){
32. metadataList.ColumnName(i));
33. System.out.ColumnName(i) + " "); //name
34. System.out.ColumnTypeName(i) + " "); //type
35. System.out.print(metadata.isNullable(i) + " "); //null
36. System.out.ColumnCharacterSet(i) + " "); //encode
37. System.out.println(); //key
38.
39. }
40.
41. System.out.println("====================表数据===========================
==");
42. Iterator<String> i = null;
43. String oneKey = null;
44. ()){
45. i = metadataList.iterator();
46. while(i.hasNext()){
47. oneKey = i.next();
48. System.out.print(oneKey + ":" + String(oneKey) + " ");
49. }
50. System.out.println();
51. }
52.
53. result.close();
54. statement.close();
55. con.close();
56.
57. } catch (ClassNotFoundException e) {
58. e.printStackTrace();
59. } catch (SQLException e) {
60. e.printStackTrace();
61. }
62.
63. }
64.
65. }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论