BDD w/ SpecFlow


Awesome tutorial on how to implement Behaviour Driven Development using SpecFlow

Advertisements

Setting up a basic JQuery Dialog [Part 1]


This will be part 1 of a multiple part blog. In part I will discuss how to implement a dialog using jQuery and JavaScript.

English: jQuery Mobile logo.

English: jQuery Mobile logo. (Photo credit: Wikipedia)

To implement a dialog , I have the following code structure:

<html>

<head>

<!– Implement  script here–>

</head>

<body>

<!– implement –>div to bind jQuery dialog to–>

</body>

</html>

We will start with the script portion located between the tags, note you can place the script in the head or in the body.

Here is the script below:

<script type=”/text/javascript”>

$(“#div_dialog”).dialog({
modal: true,
buttons: {
Ok: function () {
$(this).dialog(“close”);
}
}
});

$(“#opener”)
.button()
.click(function () {
$(“#div_dialog”).dialog(“open”);
});

</script>

This is a basic dialog declaration, first a few things,  #div_dialog refers to a div we will place in the body. This is a modal dialog, meaning that it prevents the user from interacting with the rest of the page while the dialog is open. The OK button closes the dialog.

Now for the div declaration in the body of the html. The div is needed because like most jQuery structures they need a div or an html element to anchor or bind to.

Simply place this is the body:

<div id=”div_dialog“>

</div>

you don’t had to give it an id of div_dialog,  you can name it whatever you please. We will also add a button to trigger the dialog to open.

Simply place this code in the body as well:

Add the following to your script:

$(“#opener”)
.button()
.click(function () {
$(“#div_dialog”).dialog(“open”);
});
We are simply adding a jQuery function that says when the opener button is clicked  open the dialog.
Here is the complete code below or a sexier formatted version at JSFIDDLE : Basic Jquery dialog

<html>

<head>

<script type=”/text/javascript”>

$(“#div_dialog”).dialog({
modal: true,
buttons: {
Ok: function () {
$(this).dialog(“close”);
}
}
});

$(“#opener”)
.button()
.click(function () {
$(“#div_dialog”).dialog(“open”);
});

</script>

</head>

<body>

<div id=”div_dialog“>

<h2>This is a dialog</h2>

</div>

<button type=”button”  id=”opener”>Open dialog!!</button>

</body>

</html>

That should be all you need for a simple dialog. In part 2 I will show you how to make a useful dialog with a form to gather information.

Cheers

Basic Java SQL DB interface


———————————————————————————————————

Here is little code snippet to connect to; insert,delete to/from a database.

The class can be instantiated :

Process proc = new Process();

proc.init();

//This is a database that has 3 columns : emails, password , content(string)

proc.insertData(email,password,uristream)

proc.close(); // close connection when we are done

———————————————————————————————————

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

class Process{
private String table;
Connection conn;
Statement stmt;

private final String url = “jdbc:mysql://:/”;
private final String password =”password”;
private final String userName = “root”;
private final String dbName = “testdb”;

Process(){
}

//Provided when db is not hardcoded into source
Process(String dbName,String userName, String password){
initDB(dbName,userName,password);
}

void selectTable(String thistable ){
table = thistable;
}

String getTable(){
return table;
}

void initDB(String dbName,String userName, String password){
try
{
// Load the jdbc driver class
Class.forName( “com.mysql.jdbc.Driver” ).newInstance();

// Then we ask a connection from the DriverManager by passing the
// connection URL and the password.
conn = DriverManager.getConnection( url+dbName,userName,password);
stmt = conn.createStatement();
System.out.println(“connection!”);
}
catch( Exception e )
{
System.out.println(“Could not init()” );
System.out.println( e.getMessage() );
e.printStackTrace();
}
}

void initDB(){
try
{
// Load the jdbc driver class
Class.forName( “com.mysql.jdbc.Driver” ).newInstance();

// Then we ask a connection from the DriverManager by passing the
// connection URL and the password.
conn = DriverManager.getConnection( url+dbName,userName,password);
stmt = conn.createStatement();
System.out.println(“connection!”);
}
catch( Exception e )
{
System.out.println(“Could not init()” );
System.out.println( e.getMessage() );
e.printStackTrace();
}
}

//INSERT INTO table_name VALUES (value1, value2, value3,…)

void insertData(String email,String password,String uristream){

try{
String query = “INSERT INTO “+getTable()+” VALUES (?,?,?)”;
PreparedStatement stmt= conn.prepareStatement(query);
stmt.setString(1, email);
stmt.setString(2,password);
stmt.setString(3,uristream);
stmt.executeUpdate();
}
catch(SQLException e){
System.out.println( “SQLException: Unable to insert” );
System.out.println( “SQLException: ” + e.getMessage() );
System.out.println( “SQLState: ” + e.getSQLState() );
System.out.println( “VendorError: ” + e.getErrorCode() );
}

}

//DELETE FROM table_name WHERE some_column=some_value
void deleteData(String colName, String value){

// To delete records from tables we create an SQL delete command.
// The question mark that we used in the where clause will be the
// holder of value that will be assigned by PreparedStatement class
String query = “DELETE FROM “+getTable()+” WHERE “+colName+” = ?”;

// Create a statement object. We use PreparedStatement here.

try {
PreparedStatement stmt = conn.prepareStatement(query);

// Pass a value of a the variable that will tell the database which
// records in the database to be deleted. Remember that when
// using a statement object the index parameter is start from
// 1 not 0 as in the Java array data type index
stmt.setString(1,value);

// Tell the statement to execute the command. The executeUpdate()
// method for a delete command returns number of records deleted
// as the command executed in the database. If no records was
// deleted it will simply return 0
int rows = stmt.executeUpdate();

System.out.println(rows + ” record(s) deleted from “+getTable());
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println( “SQLException: Unable to delete” );
e.printStackTrace();
}

}
void closeDb(){

try {
if (conn != null && !conn.isClosed())
{
conn.close();
System.out.print(“Db connection terminated”);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}