Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.39 KB | None | 0 0
  1. package pl.edu.pw.elka.bd.lab6;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DatabaseMetaData;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.Scanner;
  10.  
  11. import oracle.jdbc.pool.OracleDataSource;
  12.  
  13.  
  14. /**
  15. * BD.A
  16. * lab 6
  17. * JDBC Showcase.
  18. *
  19. * @author B.Twardowski <B.Twardowski@ii.pw.edu.pl>
  20. *
  21. */
  22. public class JDBCShowcase {
  23.  
  24. //const's
  25. private static String JDBC_URL = "jdbc:oracle:thin:mjarzebs/mjarzebs@ora3.elka.pw.edu.pl:1521:ora3inf";
  26.  
  27.  
  28. private OracleDataSource ods;
  29. private Connection conn;
  30. private Scanner inputScanner = new Scanner(System.in);
  31.  
  32.  
  33.  
  34. /**
  35. * Initializing DB connection.
  36. * @throws SQLException
  37. */
  38. private void init() throws SQLException {
  39. ods = new OracleDataSource();
  40. ods.setURL(JDBC_URL);
  41. conn = ods.getConnection();
  42. DatabaseMetaData meta = conn.getMetaData();
  43. System.out.println("Successfully connected to DB. JDBC driver version is " + meta.getDriverVersion());
  44.  
  45. }
  46.  
  47. /**
  48. * Closing DB connection.
  49. * @throws SQLException
  50. */
  51. private void close() throws SQLException {
  52. conn.close();
  53. System.out.println("Database connection successfully closed.");
  54. }
  55.  
  56.  
  57. /**
  58. * Showcase.
  59. * @throws SQLException
  60. */
  61. public void doShowcase() throws SQLException {
  62. init();
  63. int i=5;
  64. while(i!=0)
  65. {
  66. System.out.println("0- Exit 1- Create 2- Read 3-Update 4-Delete");
  67. i= inputScanner.nextInt();
  68. inputScanner.nextLine();
  69. System.out.println("tst");
  70.  
  71. if(i==1)
  72. {
  73. Create();
  74. }
  75. if(i==2)
  76. {
  77. Read();
  78. }
  79. if(i==3)
  80. {
  81. Update();
  82. }
  83. if(i==4)
  84. {
  85. Delete();
  86. }
  87.  
  88. }
  89.  
  90.  
  91.  
  92.  
  93.  
  94. /*simpleTest();
  95. preparedStatementShowcace();
  96. transactionShowcace();*/
  97. close();
  98. }
  99.  
  100.  
  101.  
  102. private void Delete() throws SQLException {
  103. System.out.println("Podaj nip do usuniecia");
  104. PreparedStatement preparedStatement = conn.prepareStatement("delete from firmy where nip=?");
  105. preparedStatement.setString(1, inputScanner.nextLine() );
  106. ResultSet rs = preparedStatement.executeQuery();
  107.  
  108. /*int j = 1;
  109. while (rs.next()) {
  110. System.out.println("[" + j + "]:" + rs.getString(1));
  111. j++;
  112. }*/
  113.  
  114. // close the result set, the statement and connect
  115. rs.close();
  116. preparedStatement.close();
  117. }
  118.  
  119.  
  120.  
  121.  
  122. private void Update() throws SQLException {
  123. System.out.println("Podaj nazwe firmy");
  124. PreparedStatement preparedStatement = conn.prepareStatement("update firmy set nip=? where nazwa=?");
  125. preparedStatement.setString(2, inputScanner.nextLine() );
  126. System.out.println("Podaj nowy nip firmy");
  127. preparedStatement.setString(1, inputScanner.nextLine() );
  128. ResultSet rs = preparedStatement.executeQuery();
  129.  
  130. /*int j = 1;
  131. while (rs.next()) {
  132. System.out.println("[" + j + "]:" + rs.getString(1));
  133. j++;
  134. }*/
  135.  
  136. // close the result set, the statement and connect
  137. rs.close();
  138. preparedStatement.close();
  139. }
  140.  
  141.  
  142.  
  143. private void Create() throws SQLException {
  144. System.out.println("Podaj nazwe firmy");
  145. PreparedStatement preparedStatement = conn.prepareStatement("insert into firmy values (?,?)");
  146. preparedStatement.setString(1, inputScanner.nextLine() );
  147. System.out.println("Podaj nip firmy");
  148. preparedStatement.setString(2, inputScanner.nextLine() );
  149. ResultSet rs = preparedStatement.executeQuery();
  150.  
  151. /*int j = 1;
  152. while (rs.next()) {
  153. System.out.println("[" + j + "]:" + rs.getString(1));
  154. j++;
  155. }*/
  156.  
  157. // close the result set, the statement and connect
  158. rs.close();
  159. preparedStatement.close();
  160. }
  161.  
  162.  
  163.  
  164.  
  165. private void Read() throws SQLException {
  166. System.out.println("Podaj nazwe firmy ktorej nip chcesz odczytac");
  167. PreparedStatement preparedStatement = conn.prepareStatement("select nip from firmy where nazwa like ?");
  168. preparedStatement.setString(1, inputScanner.nextLine() );
  169. ResultSet rs = preparedStatement.executeQuery();
  170.  
  171. int j = 1;
  172. while (rs.next()) {
  173. System.out.println("[" + j + "]:" + rs.getString(1));
  174. j++;
  175. }
  176.  
  177. // close the result set, the statement and connect
  178. rs.close();
  179. preparedStatement.close();
  180. }
  181.  
  182.  
  183.  
  184. /**
  185. * Simple select statement.
  186. * @throws SQLException
  187. */
  188. private void simpleTest() throws SQLException {
  189.  
  190. System.out.println("Simple query statement test...");
  191.  
  192. // Create a statement
  193. Statement stmt = conn.createStatement();
  194.  
  195. // Execute SQL
  196. ResultSet rset = stmt.executeQuery("select pesel from kierowcy");
  197.  
  198. System.out.println("Query result: ");
  199.  
  200. int i = 1;
  201. while (rset.next()) {
  202. System.out.println("[" + i + "]:" + rset.getString(1));
  203. //System.out.println("[" + i + "]:" + rset.getString(2));
  204. i++;
  205. }
  206.  
  207. // close the result set, the statement and connect
  208. rset.close();
  209. stmt.close();
  210.  
  211. }
  212.  
  213.  
  214. /**
  215. * Prepared statement showcase.
  216. * @throws SQLException
  217. */
  218. private void preparedStatementShowcace() throws SQLException {
  219.  
  220. System.out.println("Prepared statement showcase...");
  221.  
  222. //TODO: To modify!!!
  223.  
  224. PreparedStatement preparedStatement = conn.prepareStatement("select pesel from kierowcy where imie like ?");
  225.  
  226. //get params from console
  227. System.out.println("Type employee name :" );
  228. preparedStatement.setString(1, inputScanner.nextLine() );
  229.  
  230. ResultSet rs = preparedStatement.executeQuery();
  231.  
  232. int i = 1;
  233. while (rs.next()) {
  234. System.out.println("[" + i + "]:" + rs.getString(1));
  235. i++;
  236. }
  237.  
  238. // close the result set, the statement and connect
  239. rs.close();
  240. preparedStatement.close();
  241.  
  242. }
  243.  
  244.  
  245.  
  246. /**
  247. * Transaction showcase.
  248. * @throws SQLException
  249. */
  250. private void transactionShowcace() throws SQLException {
  251.  
  252. System.out.println("Transactions statement showcase...");
  253.  
  254. //TODO: To fill...
  255.  
  256. conn.setAutoCommit(false);
  257.  
  258. //operations - selects/updates/inserts with user interactions
  259.  
  260. //if something wrong
  261. //conn.rollback();
  262.  
  263.  
  264. //if no error
  265. //conn.commit();
  266.  
  267. }
  268.  
  269.  
  270. /**
  271. * Run showcase.
  272. *
  273. * @param args
  274. */
  275. public static void main(String[] args) throws SQLException {
  276. JDBCShowcase showcase = new JDBCShowcase();
  277. showcase.doShowcase();
  278. }
  279.  
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement