Advertisement
Guest User

Untitled

a guest
Sep 20th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1.  
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.text.DateFormat;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9.  
  10. public class testDB {
  11.  
  12. static Connection connection = null;
  13.  
  14. public static void main(String[] argv) {
  15.  
  16. if (true == connectdb()) {
  17.  
  18. DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  19. Date date = new Date();
  20. System.out.println(dateFormat.format(date)); //2014/08/06 15:59:48
  21. //System.out.println(date); //2014/08/06 15:59:48
  22.  
  23.  
  24. String createTableSyntax = " create table transaction "
  25. + "("
  26. + "TID NUMBER,"
  27. + "AccountNumber number,"
  28. + "Amount number,"
  29. + "type number,"
  30. + "timestamp varchar2(21)"
  31. + ")";
  32. Statement statement = null;
  33.  
  34. try {
  35. statement = connection.createStatement();
  36.  
  37. for(int i=1; i<=10; i++)
  38. {
  39. String syntax = null;
  40. syntax= "INSERT INTO transaction VALUES (" + (i+100) + ", " + (144416+i) + "," + 5000 +","+ "0"+ ",'"+ dateFormat.format(date) + "' )";
  41. //syntax = "insert into transaction values (1, 144418, 1000, 0, 'jan1000')";
  42. statement.execute(syntax);
  43. }
  44.  
  45. } catch (SQLException e) {
  46. e.printStackTrace();
  47. }
  48.  
  49. }
  50.  
  51. }
  52.  
  53. public static boolean connectdb() {
  54. System.out.println("-------- Oracle JDBC Connection Testing ------");
  55. try {
  56. Class.forName("oracle.jdbc.driver.OracleDriver");
  57. } catch (ClassNotFoundException e) {
  58. System.out.println("Where is your Oracle JDBC Driver?");
  59. e.printStackTrace();
  60. //return;
  61. }
  62. System.out.println("Oracle JDBC Driver Registered!");
  63.  
  64. try {
  65.  
  66. connection = DriverManager.getConnection(
  67. "jdbc:oracle:thin:@localhost:1521:XE", "test", "test");
  68.  
  69. } catch (SQLException e) {
  70.  
  71. System.out.println("Connection Failed! Check output console");
  72. e.printStackTrace();
  73. //return;
  74. }
  75.  
  76. if (connection != null) {
  77. System.out.println("You made it, take control your database now!");
  78. return true;
  79. } else {
  80. System.out.println("Failed to make connection!");
  81. return false;
  82. }
  83. }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement