Pages

Saturday, 25 June 2011

Enabling Query Cache for fast query execution in MySQL

1. Open the MySQL Console

2. Enter SET GLOBAL query_cache_size = 1048576;
(Size is in KBs, 1GB = 1x1024x1024=1048576)

3. Other Cache Related Settings includes
SET GLOBAL query_cache_type=1;
(To Enable Query Cache)
SET GLOBAL query_cache_limit=1024;
(Per User 1MB 1x1024)

4. Caching speeds up the fetching of queries, the size must be chosen according as the RAM size

5. To check their values, you can run command
SHOW VARIABLES LIKE 'query%';

Note: To run mysql in terminal: mysql -u root -p

Tuesday, 14 June 2011

Animation Effect on your Splash/Welcome Screen on iPhone

In this tutorial, I'll give an example of how to have your own welcome image, and add the fade and zoom effect to it. When the iPhone launches your application, it looks for an image named Default.png in the resources directory. In the following example, Default.png is being replaced by a UIImageView which zooms and then fades away. (Consider abc to be your project name)

1. To abcAppDelegate.h, add the function declaration

- (void) welcomeScreen

2. To abcAppDelegate.m, add the function definition


- (void) welcomeScreen
{
//Welcome Screen
UIImageView* welcome = [[[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)]autorelease];
welcome.image = [UIImage imageNamed:@"img.png"];
[window addSubview:welcome];
[window bringSubviewToFront:welcome];
//Animation Effects (zoom and fade)
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:window cache:YES];
[UIView setAnimationDelegate:welcome];
[UIView setAnimationDidStopSelector:@selector(removeFromSuperview)];

//set transparency to 0.0
welcome.alpha = 0.0;

//zoom effect
welcome.frame = CGRectMake(-60, -60, 440, 600);
[UIView commitAnimations];

}

3. Call the method welcomeScreen

From applicationDidFinishLaunching method in abcAppDelegate.m, invoke welcomeScreen.

- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:viewController.view];
[window makeKeyAndVisible];
[NSTimer scheduledTimerWithTimeInterval:1.0/30. target:self selector:@selector(welcomeScreen) userInfo:nil repeats:NO];

}

And it's a wrap! Cheers!



Monday, 6 June 2011

JDBC and MySQL

Steps to connect to a MySQL database from Java


1. Import

import java.sql.*;
import javax.sql.*;

2. Declare a Connection type in the class:

Connection conn;

3. Add a method to establish connection. Assume the name of the database is GameIF, and the username/password are the default values i.e username is root and there is no password.


private void connect()
{
String dbUrl = "jdbc:mysql://localhost/GameIF";
String dbClass = "com.mysql.jdbc.Driver";
String username="root";
String password="";

try
{
Class.forName(dbClass).newInstance();
conn = DriverManager.getConnection(dbUrl,username,password);
}
catch(Exception e)
{
System.out.println(e);
}
}



3.  Add a function that tears down the connection:


private void disconnect()
{
if (conn != null)
{
try
{
conn.close();
}
catch (Exception e) {}
}
}


4. Writing/Executing a query: A query may be of two types:

(a) One that returns a result set (e.g: SELECT query)

(b) One that returns no result set (e.g: DROP, CREATE, INSERT, UPDATE)

We'll see examples for both types:

(a) SELECT

connect( );

String uname, steps;

String query = "select * from `scores`";
try
{
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
while(rs.next())
{
uname = rs.getString("username");
steps = rs.getInt("steps");
System.out.println("Username: "+uname+"\tSteps: "+steps);
}
rs.close();
st.close();
}
catch(Exception e) {
System.out.println(e);
}


disconnect( );


(b) INSERT


connect( );


Statement s;
String query = "insert into `scores` (`game` , `username` , `steps`) values ('NUMBERJUMBLE','Tups',56)";
try
{
s = conn.createStatement( );
s.executeUpdate(query);
s.close( );
}
catch(Exception e)
{
System.out.println(e);
}


disconnect( );




And, we're done!
However, a very common exception is encountered if you are building a standalone application:

java.lang.ClassNotFoundException

To fix this:
1. Download the mysql connector jar files (A straightforward google search!)
2. Add the jar files (format mysql-connector-java-<version>-bin.jar) to the following two folders:
         <your jdk directory>/jre/lib
         <your jdk directory>/jre/lib/ext

If you are using netbeans or another IDE, copy the files and restart!

Cheers!


Saturday, 4 June 2011

Add sound on Jbutton click in Java

To add a sound on JButton click event, follow three simple steps:
1. Import the following:

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.Clip;
import javax.sound.sampled.AudioSystem;

2. Add the following function to your class:

public void playSound(String soundName)
{
try
{
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(soundName).getAbsoluteFile( ));
Clip clip = AudioSystem.getClip( );
clip.open(audioInputStream);
clip.start( );
}
catch(Exception ex)
{
System.out.println("Error with playing sound.");
ex.printStackTrace( );
}
}

3. In your actionPerformed function for the JButton, just call the function with the filename as string:

public void actionPerformed(ActionEvent ae)
{

//do something
playSound("buzzer.wav");
}




About

I often came across broken or unhelpful snippets while working on projects. A small feature required by a project needed hours of surfing on the net. Just wanted to make things a tad simpler for you!