Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. package com.XXX.Tests;
  2.  
  3. import java.sql.*;
  4. import java.sql.Connection;
  5. import org.junit.Test;
  6. import org.testng.annotations.BeforeClass;
  7. import com.thoughtworks.selenium.*;
  8.  
  9. import org.openqa.selenium.server.SeleniumServer;
  10.  
  11.  
  12. public class SeleniumandDB extends SeleneseTestBase {
  13.  
  14. @BeforeClass
  15. public void setUp()throws Exception {
  16.  
  17. SeleniumServer seleniumServer=null;
  18. try {
  19. seleniumServer = new SeleniumServer();
  20. seleniumServer.start();
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
  24.  
  25. selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://wwww-test/");
  26. selenium.start();
  27. }
  28.  
  29.  
  30.  
  31.  
  32. @Test public void testUntitled2() throws Exception {
  33. String userID = null;
  34. Connection conn=null;
  35. Statement stmt=null;
  36. ResultSet rs=null;
  37.  
  38.  
  39. selenium.open("/");
  40. selenium.windowFocus();
  41. selenium.windowMaximize();
  42.  
  43. Class.forName("net.sourceforge.jtds.jdbc.Driver");
  44. conn = DriverManager.getConnection("jdbc:jtds:sqlserver://XXXX:1433/XXX","XX","XXXX");
  45. stmt = conn.createStatement();
  46. rs = stmt.executeQuery("SELECT TOP 1 UserID FROM webuser ORDER BY 1 DESC");
  47. while(rs.next()){
  48. userID = rs.getString("UserID");
  49. conn.close();
  50. System.out.println(userID);
  51.  
  52. selenium.type("txtUserID", userID);
  53. selenium.type("txtPassword", "password");
  54. selenium.click("btnLogin2");
  55. selenium.waitForPageToLoad("30000");
  56. selenium.stop();
  57.  
  58.  
  59. }
  60. }
  61. }
  62.  
  63. package DBCONN;
  64.  
  65. import org.junit.After;
  66. import org.junit.Before;
  67. import org.junit.Test;
  68. import java.sql.Connection;
  69. import java.sql.DriverManager;
  70. import java.sql.ResultSet;
  71. import java.sql.Statement;
  72.  
  73. public class DBCONN {
  74.  
  75. public static void main(String[] args) throws Exception {
  76. DBCONN dbconn = new DBCONN();
  77. dbconn.open();
  78. dbconn.run();
  79. dbconn.close();
  80. }
  81.  
  82. // Connection object
  83. static Connection con = null;
  84. // Statement object
  85. private static Statement stmt;
  86. // Constant for Database URL
  87. public static String DB_URL = "jdbc:oracle:thin:@hostname:Port#:SID";
  88. // Constant for Database Username
  89. public static String DB_USER = "username";
  90. // Constant for Database Password
  91. public static String DB_PASSWORD = "password";
  92.  
  93. public static String query = "SELECT * FROM TABLE;
  94.  
  95. public void open() throws Exception {
  96. try {
  97. // Make the database connection
  98. String dbClass = "oracle.jdbc.OracleDriver";
  99. System.out.println("Connecting to database");
  100. Class.forName(dbClass).newInstance();
  101. // Get connection to DB
  102. con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
  103. // Statement object to send the SQL statement to the Database
  104. System.out.println("Connected to the database");
  105. stmt = con.createStatement();
  106. } catch (Exception e) {
  107. con.close();
  108. System.out.println("Closed connection to the database");
  109. e.printStackTrace();
  110.  
  111. }
  112. }
  113.  
  114. public void run() throws Exception {
  115. try {
  116.  
  117. ResultSet res = stmt.executeQuery(query);
  118. res.next();
  119. System.out.print(res.getString(1));
  120.  
  121. System.out.print("t" + res.getString(2));
  122.  
  123. System.out.print("t" + res.getString(3));
  124.  
  125. System.out.println("t" + res.getString(4));
  126.  
  127. } catch (Exception e) {
  128. con.close();
  129. System.out.println("Closed connection to the database");
  130. e.printStackTrace();
  131.  
  132. }
  133. }
  134.  
  135. public void close() throws Exception {
  136. try {
  137.  
  138. con.close();
  139. System.out.println("Closed connection to the database");
  140.  
  141. } catch (Exception e) {
  142. System.out.println("Error closing connection to the database");
  143. e.printStackTrace();
  144.  
  145. }`enter code here`
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement