Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.XXX.Tests;
- import java.sql.*;
- import java.sql.Connection;
- import org.junit.Test;
- import org.testng.annotations.BeforeClass;
- import com.thoughtworks.selenium.*;
- import org.openqa.selenium.server.SeleniumServer;
- public class SeleniumandDB extends SeleneseTestBase {
- @BeforeClass
- public void setUp()throws Exception {
- SeleniumServer seleniumServer=null;
- try {
- seleniumServer = new SeleniumServer();
- seleniumServer.start();
- } catch (Exception e) {
- e.printStackTrace();
- }
- selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://wwww-test/");
- selenium.start();
- }
- @Test public void testUntitled2() throws Exception {
- String userID = null;
- Connection conn=null;
- Statement stmt=null;
- ResultSet rs=null;
- selenium.open("/");
- selenium.windowFocus();
- selenium.windowMaximize();
- Class.forName("net.sourceforge.jtds.jdbc.Driver");
- conn = DriverManager.getConnection("jdbc:jtds:sqlserver://XXXX:1433/XXX","XX","XXXX");
- stmt = conn.createStatement();
- rs = stmt.executeQuery("SELECT TOP 1 UserID FROM webuser ORDER BY 1 DESC");
- while(rs.next()){
- userID = rs.getString("UserID");
- conn.close();
- System.out.println(userID);
- selenium.type("txtUserID", userID);
- selenium.type("txtPassword", "password");
- selenium.click("btnLogin2");
- selenium.waitForPageToLoad("30000");
- selenium.stop();
- }
- }
- }
- package DBCONN;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- public class DBCONN {
- public static void main(String[] args) throws Exception {
- DBCONN dbconn = new DBCONN();
- dbconn.open();
- dbconn.run();
- dbconn.close();
- }
- // Connection object
- static Connection con = null;
- // Statement object
- private static Statement stmt;
- // Constant for Database URL
- public static String DB_URL = "jdbc:oracle:thin:@hostname:Port#:SID";
- // Constant for Database Username
- public static String DB_USER = "username";
- // Constant for Database Password
- public static String DB_PASSWORD = "password";
- public static String query = "SELECT * FROM TABLE;
- public void open() throws Exception {
- try {
- // Make the database connection
- String dbClass = "oracle.jdbc.OracleDriver";
- System.out.println("Connecting to database");
- Class.forName(dbClass).newInstance();
- // Get connection to DB
- con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
- // Statement object to send the SQL statement to the Database
- System.out.println("Connected to the database");
- stmt = con.createStatement();
- } catch (Exception e) {
- con.close();
- System.out.println("Closed connection to the database");
- e.printStackTrace();
- }
- }
- public void run() throws Exception {
- try {
- ResultSet res = stmt.executeQuery(query);
- res.next();
- System.out.print(res.getString(1));
- System.out.print("t" + res.getString(2));
- System.out.print("t" + res.getString(3));
- System.out.println("t" + res.getString(4));
- } catch (Exception e) {
- con.close();
- System.out.println("Closed connection to the database");
- e.printStackTrace();
- }
- }
- public void close() throws Exception {
- try {
- con.close();
- System.out.println("Closed connection to the database");
- } catch (Exception e) {
- System.out.println("Error closing connection to the database");
- e.printStackTrace();
- }`enter code here`
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement