Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.41 KB | None | 0 0
  1. package connector;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5.  
  6. public class CronusDB {
  7.  
  8. public static Connection startConnection() {
  9. Connection con = null;
  10. try {
  11. DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
  12. } catch (Exception e) {
  13. System.out.println("Kan inte database driver class: " + e);
  14. }
  15. try {
  16. con = DriverManager.getConnection(
  17. "jdbc:sqlserver://localhost:1433;" + "databaseName=Demo Database NAV (5-0);user=sa;password=kalle;");
  18. if (con == null)
  19. System.out.println("No driver found!");
  20. else
  21. System.out.println("Connection Established - SUCCESS!");
  22.  
  23. } catch (Exception e) {
  24. System.out.println("Kaaaaaaaan inte hitta database driver class: " + e);
  25. }
  26. return con;
  27.  
  28. }
  29. }
  30.  
  31. ********************************************************************************************************
  32. package databaseAccesslayer;
  33.  
  34. import java.sql.*;
  35. import java.util.ArrayList;
  36. import java.util.Vector;
  37.  
  38. import connector.CronusDB;
  39. import connector.*;
  40.  
  41. public class CronusDal {
  42.  
  43. String sqlString;
  44.  
  45. /**
  46. * ********** MetaDataMetoder **********
  47. */
  48.  
  49. public Vector<String> getStandardMetaData(String from) throws SQLException {
  50. Vector<String> vectorList = new Vector<String>();
  51.  
  52. Connection con = CronusDB.startConnection();
  53.  
  54. sqlString = "select * from [" + from + "]";
  55.  
  56. Statement stmt = con.createStatement();
  57. ResultSet rs = stmt.executeQuery(sqlString);
  58. ResultSetMetaData rsmd = rs.getMetaData();
  59. int j = 5;
  60. for (int i = 1; i <= j; i++) {
  61. vectorList.add(rsmd.getColumnLabel(i));
  62. }
  63. return vectorList;
  64. }
  65.  
  66. public Vector<String> getStatsMetaData(String from) throws SQLException {
  67. Vector<String> vectorList = new Vector<String>();
  68.  
  69. Connection con = CronusDB.startConnection();
  70.  
  71. sqlString = "select * from [" + from + "]";
  72.  
  73. Statement stmt = con.createStatement();
  74. ResultSet rs = stmt.executeQuery(sqlString);
  75. ResultSetMetaData rsmd = rs.getMetaData();
  76. int j = rsmd.getColumnCount();
  77. for (int i = 1; i <= j; i++) {
  78. vectorList.add(rsmd.getColumnLabel(i));
  79. }
  80. return vectorList;
  81. }
  82.  
  83. // public Vector<String> getKeysMetaData() throws SQLException {
  84. // Vector<String> vectorList = new Vector<String>();
  85. //
  86. // Connection con = CronusDB.startConnection();
  87. //
  88. // sqlString = "select table_name, column_name from information_schema.key_column_usage";
  89. //
  90. // Statement stmt = con.createStatement();
  91. // ResultSet rs = stmt.executeQuery(sqlString);
  92. // ResultSetMetaData rsmd = rs.getMetaData();
  93. // int j = rsmd.getColumnCount();
  94. //
  95. // for (int i = 1; i <= j; i++) {
  96. // vectorList.add(rsmd.getColumnLabel(i));
  97. // }
  98. // return vectorList;
  99. // }
  100.  
  101. // public Vector<String> getIndexesMetaData() throws SQLException {
  102. // Vector<String> vectorList = new Vector<String>();
  103. //
  104. // Connection con = CronusDB.startConnection();
  105. //
  106. // sqlString = "select * from sys.indexes";
  107. //
  108. // Statement stmt = con.createStatement();
  109. // ResultSet rs = stmt.executeQuery(sqlString);
  110. // ResultSetMetaData rsmd = rs.getMetaData();
  111. // int j = rsmd.getColumnCount();
  112. //
  113. // for (int i = 1; i <= j; i++) {
  114. // vectorList.add(rsmd.getColumnLabel(i));
  115. // }
  116. // return vectorList;
  117. //
  118. // }
  119.  
  120. public Vector<String> getConstraintsMetaData() throws SQLException {
  121. Vector<String> vectorList = new Vector<String>();
  122.  
  123. Connection con = CronusDB.startConnection();
  124.  
  125. sqlString = "select * from information_schema.table_constraints";
  126.  
  127. Statement stmt = con.createStatement();
  128. ResultSet rs = stmt.executeQuery(sqlString);
  129. ResultSetMetaData rsmd = rs.getMetaData();
  130. int j = rsmd.getColumnCount();
  131. for (int i = 1; i <= j; i++) {
  132. vectorList.add(rsmd.getColumnLabel(i));
  133. }
  134. return vectorList;
  135. }
  136.  
  137. public Vector<String> getTablesOneMetaData() throws SQLException {
  138. Vector<String> vectorList = new Vector<String>();
  139.  
  140. Connection con = CronusDB.startConnection();
  141.  
  142. sqlString = "select * from sys.tables";
  143.  
  144. Statement stmt = con.createStatement();
  145. ResultSet rs = stmt.executeQuery(sqlString);
  146. ResultSetMetaData rsmd = rs.getMetaData();
  147.  
  148. vectorList.add(rsmd.getColumnLabel(1));
  149.  
  150. return vectorList;
  151. }
  152.  
  153. public Vector<String> getTablesTwoMetaData() throws SQLException {
  154. Vector<String> vectorList = new Vector<String>();
  155.  
  156. Connection con = CronusDB.startConnection();
  157.  
  158. sqlString = "select * from information_schema.tables";
  159.  
  160. Statement stmt = con.createStatement();
  161. ResultSet rs = stmt.executeQuery(sqlString);
  162. ResultSetMetaData rsmd = rs.getMetaData();
  163.  
  164. vectorList.add(rsmd.getColumnLabel(3));
  165.  
  166. return vectorList;
  167. }
  168.  
  169. public Vector<String> getTableMaxRowsMetaData() throws SQLException {
  170. Vector<String> vectorList = new Vector<String>();
  171.  
  172. Connection con = CronusDB.startConnection();
  173.  
  174. sqlString = "select top 1 t.name, p.rows " + "from sys.tables t join sys.partitions p "
  175. + "ON t.object_id = p.object_id " + "order by rows desc";
  176.  
  177. Statement stmt = con.createStatement();
  178. ResultSet rs = stmt.executeQuery(sqlString);
  179. ResultSetMetaData rsmd = rs.getMetaData();
  180.  
  181. int j = rsmd.getColumnCount();
  182.  
  183. for (int i = 1; i <= j; i++) {
  184. vectorList.add(rsmd.getColumnLabel(i));
  185. }
  186. return vectorList;
  187. }
  188.  
  189. /**
  190. * ********** VanligaDataMetoder **********
  191. */
  192.  
  193. public Vector<Vector<String>> getStandardData(String from) throws SQLException {
  194. Vector<Vector<String>> list = new Vector<Vector<String>>();
  195.  
  196. Connection con = CronusDB.startConnection();
  197.  
  198. sqlString = "select * from [" + from + "];";
  199.  
  200. Statement stmt = con.createStatement();
  201. ResultSet rs = stmt.executeQuery(sqlString);
  202. int j = 5;
  203.  
  204. while (rs.next()) {
  205. Vector<String> sa = new Vector<String>();
  206. for (int i = 1; i <= j; i++) {
  207. sa.add(rs.getString(i));
  208. }
  209. list.add(sa);
  210. }
  211. return list;
  212. }
  213.  
  214. public Vector<Vector<String>> getStatGroup(String from) throws SQLException {
  215. Vector<Vector<String>> list = new Vector<Vector<String>>();
  216.  
  217. Connection con = CronusDB.startConnection();
  218.  
  219. sqlString = "select * from [" + from + "];";
  220.  
  221. Statement stmt = con.createStatement();
  222. ResultSet rs = stmt.executeQuery(sqlString);
  223. int j = 3;
  224.  
  225. while (rs.next()) {
  226. Vector<String> sa = new Vector<String>();
  227. for (int i = 1; i <= j; i++) {
  228. sa.add(rs.getString(i));
  229. }
  230. list.add(sa);
  231. }
  232. return list;
  233. }
  234.  
  235. public Vector<Vector<String>> getKeys() throws SQLException {
  236.  
  237. Connection con = CronusDB.startConnection();
  238.  
  239. Vector<Vector<String>> list = new Vector<Vector<String>>();
  240.  
  241. sqlString = "select table_name, column_name from information_schema.key_column_usage";
  242.  
  243. Statement stmt = con.createStatement();
  244. ResultSet rs = stmt.executeQuery(sqlString);
  245. int j = 2;
  246.  
  247. while (rs.next()) {
  248. Vector<String> sa = new Vector<String>();
  249. for (int i = 1; i <= j; i++) {
  250. sa.add(rs.getString(i));
  251. }
  252. list.add(sa);
  253. }
  254. return list;
  255. }
  256.  
  257. public Vector<Vector<String>> getConstraints() throws SQLException {
  258. Vector<Vector<String>> list = new Vector<Vector<String>>();
  259.  
  260. Connection con = CronusDB.startConnection();
  261.  
  262. sqlString = "select * from information_schema.table_constraints";
  263.  
  264. Statement stmt = con.createStatement();
  265. ResultSet rs = stmt.executeQuery(sqlString);
  266. ResultSetMetaData rsmd = rs.getMetaData();
  267. int j = rsmd.getColumnCount();
  268.  
  269. while (rs.next()) {
  270. Vector<String> sa = new Vector<String>();
  271. for (int i = 1; i <= j; i++) {
  272. sa.add(rs.getString(i));
  273. }
  274. list.add(sa);
  275. }
  276. return list;
  277. }
  278.  
  279. public Vector<Vector<String>> getTablesOne() throws SQLException {
  280. Vector<Vector<String>> list = new Vector<Vector<String>>();
  281.  
  282. Connection con = CronusDB.startConnection();
  283.  
  284. sqlString = "select * from sys.tables";
  285.  
  286. Statement stmt = con.createStatement();
  287. ResultSet rs = stmt.executeQuery(sqlString);
  288.  
  289. while (rs.next()) {
  290. Vector<String> sa = new Vector<String>();
  291.  
  292. sa.add(rs.getString(1));
  293.  
  294. list.add(sa);
  295. }
  296. return list;
  297. }
  298.  
  299. public Vector<Vector<String>> getTablesTwo() throws SQLException {
  300. Vector<Vector<String>> list = new Vector<Vector<String>>();
  301.  
  302. Connection con = CronusDB.startConnection();
  303.  
  304. sqlString = "select * from information_schema.tables";
  305.  
  306. Statement stmt = con.createStatement();
  307. ResultSet rs = stmt.executeQuery(sqlString);
  308.  
  309. while (rs.next()) {
  310. Vector<String> sa = new Vector<String>();
  311.  
  312. sa.add(rs.getString(3));
  313.  
  314. list.add(sa);
  315. }
  316. return list;
  317. }
  318.  
  319. public Vector<Vector<String>> getIndex() throws SQLException {
  320. Vector<Vector<String>> list = new Vector<Vector<String>>();
  321.  
  322. Connection con = CronusDB.startConnection();
  323.  
  324. sqlString = "select * from sys.indexes";
  325.  
  326. Statement stmt = con.createStatement();
  327. ResultSet rs = stmt.executeQuery(sqlString);
  328. ResultSetMetaData rsmd = rs.getMetaData();
  329. int j = rsmd.getColumnCount();
  330.  
  331. while (rs.next()) {
  332. Vector<String> sa = new Vector<String>();
  333. for (int i = 1; i <= j; i++) {
  334. sa.add(rs.getString(i));
  335. }
  336. list.add(sa);
  337. }
  338. return list;
  339. }
  340.  
  341. // public Vector<Vector<String>> getEmployeeColumnsOne() throws SQLException
  342. // {
  343. // Vector<Vector<String>> list = new Vector<Vector<String>>();
  344. //
  345. // Connection con = CronusDB.startConnection();
  346. //
  347. // sqlString = "select column_name from information_schema.columns "
  348. // + "where table_name = 'CRONUS Sverige AB$Employee'";
  349. //
  350. // Statement stmt = con.createStatement();
  351. // ResultSet rs = stmt.executeQuery(sqlString);
  352. // ResultSetMetaData rsmd = rs.getMetaData();
  353. // int j = rsmd.getColumnCount();
  354. //
  355. // while (rs.next()) {
  356. // Vector<String> sa = new Vector<String>();
  357. // for (int i = 1; i <= j; i++) {
  358. // sa.add(rs.getString(i));
  359. // }
  360. // list.add(sa);
  361. // }
  362. // return list;
  363. // }
  364. //
  365. // public Vector<Vector<String>> getEmployeeColumnsTwo() throws SQLException
  366. // {
  367. // Vector<Vector<String>> list = new Vector<Vector<String>>();
  368. //
  369. // Connection con = CronusDB.startConnection();
  370. //
  371. // sqlString = "select * from [CRONUS Sverige AB$Employee]";
  372. //
  373. // Statement stmt = con.createStatement();
  374. // ResultSet rs = stmt.executeQuery(sqlString);
  375. // ResultSetMetaData rsmd = rs.getMetaData();
  376. // int j = rsmd.getColumnCount();
  377. //
  378. // for (int i = 1; i <= j; i++) {
  379. // Vector<String> sa = new Vector<String>();
  380. // sa.add(rsmd.getColumnLabel(i));
  381. // list.add(sa);
  382. // }
  383. // return list;
  384. // }
  385.  
  386. public Vector<Vector<String>> getTableMaxRows() throws SQLException {
  387. Vector<Vector<String>> list = new Vector<Vector<String>>();
  388.  
  389. Connection con = CronusDB.startConnection();
  390.  
  391. sqlString = "select top 1 t.name, p.rows " + "from sys.tables t join sys.partitions p "
  392. + "on t.object_id = p.object_id " + "order by rows desc;";
  393.  
  394. Statement stmt = con.createStatement();
  395. ResultSet rs = stmt.executeQuery(sqlString);
  396. ResultSetMetaData rsmd = rs.getMetaData();
  397. int j = rsmd.getColumnCount();
  398.  
  399. while (rs.next()) {
  400. Vector<String> sa = new Vector<String>();
  401. for (int i = 1; i <= j; i++) {
  402. sa.add(rs.getString(i));
  403. }
  404. list.add(sa);
  405. }
  406. return list;
  407. }
  408. // public void openFile(String fileName)throws Exception{
  409. // File filePath = new File("C:\\Users\\toaap\\Documents\\Databasfiler\\" +
  410. // fileName + "");
  411. // Desktop.getDesktop().open(filePath);
  412. // }
  413.  
  414. }
  415.  
  416. ********************************************************************************************************
  417. ********************************************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement