View my Blog | Favorites  | Linkedin
dummy
Home Research Linux Coding Fun Movies Pictures Contact
Rashmi
Rashmi Raj
Computer Science Department
Stanford University
Resume [PDF/DOC]
rashmi - 'at' - 'cs' - 'dot' - 'stanford' - 'dot' - 'edu'

XQuery: Java: C++: Matlab:

JDBC and MYSQL



JDBC is a middleware used with the java programming language. It allows you to connect to any database as long as the database has a JDBC driver. JDBC also provides you limited database transparency. This means that as long as you use standard SQL statements on your java programs, you have a great chance that you can use this program with very minor changes even if you changed your database.
  • Install Java and JDBC on your machine
Top
When you download the latest release of JDK, you will get JDBC as well.
$ emerge dev-java/sun-jdk



  • Install your DBMS (MYSQL)
Top
MYSQL is an open source relational database management system. A very well written document for the structure of MYSQL can be found here.
$ emerge mysql
  • Administerating your MYSQL server
Top
$ /etc/init.d/mysql start // Start your mysql server
$ mysql_install_db //Create default database and user table

This creates two default databases: mysql and test
mysql database contains user info and grant permissions as follows:

GRANT TABLE FIELDS EXPLANATION:
TABLE USER: Everything after "password" is a privelege granted with values 'Y' or 'N'.
This table controls individual user global access rights.

'host','user','password','select','insert','update','delete','index','alter'
,'create','drop','grant','reload','shutdown','process','file'

TABLE DB: This controls access of USERS to databases.

'host','db','user','select','insert','update','delete','index','alter',
'create','drop','grant'

TABLE HOST: This controls which HOSTS are allowed what global access rights.

'host','db','select','insert','update','delete','index','alter',
'create','drop','grant'


Two accounts are created with a username of root. These are superuser accounts that can do anything. The initial root account passwords are empty, so anyone can connect to the MySQL server as root without a password and be granted all privileges. Two anonymous-user accounts are created, each with an empty username. The anonymous accounts have no passwords, so anyone can use them to connect to the MySQL server. On Unix, both accounts are for connections from the local host.

$ /usr/bin/mysql - root //login to your MYSQL server as root
mysql> SET PASSWORD FOR 'root'@'localhost' =PASSWORD('your password');
mysql> DELETE FROM user WHERE User = ''; // delete default users
mysql> create database rrbv // create a temporary database
mysql> drop database test //delete default test database if you do not intend to use it
mysql> grant all on rrbv.* to rashmi@localhost identified by "passwd"
mysql> FLUSH PRIVILEGES;
mysql> show databases;
mysql> show tables from db_name;



  • Install a driver on your machine (MySQL Connector-J)
Top
$ emerge dev-java/jdbc-mysql



  • Preparing to connect to the database
Top
$ /etc/init.d/mysql start // Start the server
$ export CLASSPATH=$CLASSPATH:/usr/share/jdbc-mysql/lib/
mysql-connector-java-3.0.11-stable-bin.jar
// Add the path of the driver to your JVM



  • Connecting to the database
Top
Here is the sample code for connecting to the database.

package myjdbcproject;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class jdbcproj1 {

public static void main(String[] args) {
Connection con = null;

try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/dbname","login","passwd");
if(!con.isClosed())
System.out.println("Successfully connected to " +
"MySQL server ...");
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
} finally {
try {
if(con != null)
con.close();
} catch(SQLException e) {}
}

}
}


Computing Pagerank using Matlab

Top


Here is the sample code for computing pagerank (Markov chain) for a small webgraph (assuming that webgraph (M) can be fit into memory and it is ergodic).

function rank = pagerank(M)

[r,c] = size(M);
if (r~=c), error('Only handles square input matrices'); end

[V,D] = eig(M);
[i,j] = find(abs(D-1)<=eps);
if isempty(i), error('Unity eigenvalue not found, check input matrix'); end

rank = V(:,j)./sum(V(:,j));