Showing posts with label windows. Show all posts
Showing posts with label windows. Show all posts

Monday, December 27, 2010

JDBC with mysql

What are the pre-requirements for starting JDBC?


Mysql server can be started on windows using following command

NET START mysql

Mysql server can be stopped on windows using following command

NET STOP mysql

There is another way to start stop mysql on windows,right-click MyComputer manage-->Services and Applications-->Services find MySQL and toggle start stop or restart.

After downloading connector/j the mysql-connector-java-5.1.14-bin.jar file can be used as an external jar file to the JDBC project. Please refer previously written post about adding external jars for java or post for adding external jars in eclipse.

what is jdbc?

JDBC(java Database Connectivity) is Java application programming interface that allows the Java programmers to access database management system from Java code.

Creating a database with jdbc sample code

package pac1;

import java.io.*;
import java.sql.*;

public class CreateDatabase {
 public static void main(String[] args) {
  System.out.println("Database creation example!");
  Connection con = null;// Connection is an interface in java.sql package
        // that specifies connection with specific
        // database like MySql. The SQL statements are
        // executed within the context of the Connection
        // interface.
  try {
   Class.forName("com.mysql.jdbc.Driver");// this static method
             // attempts to load the
             // class and returns class
             // instance and takes string
             // type value (driver) after
             // that matches class with
             // given string
   // DriverManager is a class of java.sql package that controls a set
   // of JDBC drivers. Each driver has to be register with this class.
   con = DriverManager.getConnection("jdbc:mysql://localhost:3306",
     "root", "123");// establishes a connection to specified
         // database url
   try {
    // Statement an interface. Statement object executes the SQL
    // statement and returns the result it produces
    Statement st = con.createStatement();// method of Connection
              // interface. which
              // returns Statement
              // object. This method
              // will compile again
              // and again whenever
              // the program runs.
    BufferedReader bf = new BufferedReader(new InputStreamReader(
      System.in));
    System.out.println("Enter Database name:");
    String database = bf.readLine();
    st.executeUpdate("CREATE DATABASE " + database);// method also
                // executes SQL
                // statement
                // that may be
                // INSERT,
                // UPDATE OR
                // DELETE
                // statement are
                // used in the
                // code. It
                // takes string
                // types
                // parameters
                // for SQL
                // statement. It
                // returns int.
    System.out.println("1 row(s) affacted");
    con.close();// disconnecting from the connection, frees all the
       // resources occupied by the database.
   } catch (SQLException s) {
    System.out.print(s.getMessage());
    System.out.println("SQL statement is not executed!");
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}



Connecting with a database sample code

import java.sql.*;

public class MysqlConnect{
  public static void main(String[] args) {
    System.out.println("MySQL Connect Example.");
    
    try {
      Class.forName("com.mysql.jdbc.Driver");
      Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest","root","root");
      System.out.println("Connected to the database");
      conn.close();
      System.out.println("Disconnected from database");
   
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}


Note that newly created databases get saved in C:\ProgramData\MySQL\MySQL Server 5.5\data.

Sunday, December 12, 2010

Dealing with external jar files on command prompt

I had a problem with compiling and running a java file in command line where it imports an external jar files. Here is the solution I've found.

Compilation

javac -cp <path to where the source codes resides>;<path to where the jar file resides> Example.java

run

java <path to where the source codes resides>;<path to where the jar file resides> Example <command-line arguments if any>

Note 1:  separator ; for windows and : for unix
Note 2:  multiple jar files can be used delimited by ;
Note 3:  . can be used to specify the current directory

How to run a jar file with command line arguments

  1. Open up a command prompt.
  2. Go to the directory which jar file resides using the command "cd".
  3. Type the following command to execute jar file with command line arguments.
    • java -jar <name of the jar file> <command line argument1> <command line argument2>


Friday, October 15, 2010

How to find the process running on port 8008?

1. Open a command prompt.

2. Type netstat -o -n -a | findstr 127.0.0.1:8008

3. Find the last column in the displayed result. It is the PID of the process which listening to port 8008.

4. Open task manager and inside process tab find PID column. If you cannot see PID column, go to view>select columns and check PID check-box.

5. Arrange processes by PID then find the PID resulted previously and see the corresponding process.