Pages

Ads 468x60px

Saturday, October 18, 2014

Java: Connection with MySQL using JDBC


This will help you create connection with MySQL database using Java (JDBC). You have to update database URL, username and password in the code, then run it you will get connection established as output.  Please include mysql-connector-java-5.1.23-bin.jar in your project build path. You can download it from here: http://www.java2s.com/Code/JarDownload/mysql/mysql-connector-java-5.1.23-bin.jar.zip



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

public class DBConnection {

public static void main(String[] args){
getConnection();
}

    public static Connection getConnection(){
    Connection conn = null;
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost/database_name";
            String user = "root";
            String password = "";
            conn =  DriverManager.getConnection(url, user, password);
            System.out.println("Connection established");
        }
        catch(ClassNotFoundException e)
        {
            System.out.println(e.getMessage());
            System.exit(0);
        }
        catch(SQLException e)
        {
            System.out.println(e.getMessage());
            System.exit(0);
        }
        return conn;
    }
}

Thursday, September 25, 2014

Android: Show Input Dialog




This code simply shows how to use an input dialog box and get input value from it.


public void showInputDialog(Context context){
AlertDialog.Builder alert = new AlertDialog.Builder(context);

alert.setTitle("Input");
alert.setMessage("Enter Text");

// Set an EditText view to get user input
final EditText input = new EditText(context);
input.setInputType(InputType.TYPE_CLASS_TEXT);
alert.setView(input);

alert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
String value = input.getText().toString().trim();
System.out.println(value);
}
});

alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// Canceled.
}
});

alert.show();
}

Java: Find Open Ports in a Range




This code will help you to find system available open ports in a range. You can use it in Java or Android applications according to the scenario.



import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;


public class SystemPorts {

public static void main(String[] args){
getFreePorts();
}

public static void getFreePorts() {

final int MAXIMUM_PORT_RANGE = 6000;
final int MINIMUM_PORT_RANGE = 5000;
int[] ports = new int[1001];
ArrayList<Integer> avialablePorts = new ArrayList<Integer>();

for (int j = 0, i = MINIMUM_PORT_RANGE; i <= MAXIMUM_PORT_RANGE ; i++, j++) {
ports[j] = i;
}

for (int i=0; i<ports.length; i++) {
try {
ServerSocket socket = new ServerSocket(ports[i]);
socket.close();
if(socket.isClosed()){
avialablePorts.add(ports[i]);
System.out.println("Port Number: " + ports[i]);
}
} catch (IOException ex) {
continue; // try next port
}
}
}

}