Archive for the ‘Programming’ Category

Java with JDBC

Combination of java and JDBC is very useful to lets the programmer run his/her program on different platforms. Java programs are secure, robust, automatically downloaded from the network.JDBC API enables Java applications to interact with different types of databases.

Some of the advantages of using Java with JDBC are
• Easy and economical
• Continued usage of already installed databases
• Development time is short
• Installation and version control simplified

JDBC does the following three things
• Establish connection with a database
• Send SQL statements
• Process the results

Popularity: 2% [?]

Java Networking

Network is nothing but a set of computers which are physically connected together.internet is a network of networks.

Protocols:
Different networks requires certain set of rules called protocols.java networking is done using TCP/IP protocol

Different types of protocols are also available in this protocol they are:

*Http(Hyper Text Transfer Protocol)
*FTP(File Transfer Protocol)
*SMTP(Simple Mail Transfer Protocol)
*NNTP(Network News Transfer Protocol)

Sockets:
Sockets is used to plug in just like a electronic sockets.it is essential that they should follow a set of rules to communicate called protocols.
TCP/IP as protocol for communication and IP addresses are the address of the sockets.

Client/Server:
A computer request some service from another computer,is called a client.one that processes the request is called server.A server waits till one of its clients makes a request.it can accept multiple connections at a time.Multi-threading is used to serve multiple users at the same time.

Internet Addresses:

Every computer are connected to a network has a unique IP address.An IP address is a 32-bit number which has four numbers separate by periods.it is possible to connect to the Internet either directly or by using internet service provider.By connecting directly to the internet,the computer is assigned with a permanent IP address.

InetAddress:
InetAddress is one class,which is used to encapsulate th IP address and the DNS.to create a instance of InetAddress class,factory methods are used as there are no visible constructors available for this class.

Methods:

static InetAddress getLocalHost()
//Returns InetAddress object representing local host
static InetAddress getByName(String hostname)
//Returns InetAddress for the host passed to it.

Popularity: 1% [?]

How do you create a Thread

Threads:
Thread is a line of execution.In a single-threaded system there is only one execution line (i.e) only one part of program is in the process of execution at any one time

To create a new thread:

Thread mythread = new thread(this);

this referes the current applet & mythread is a variable

Example:

import java.awt.*;
import java.applet.Applet;
public class MovingBall extends Applet implements runnable
{
Thread mythread=null;
int position=0;
public void start()
{
mythread=new Thread(this);
mythread.start();
}
public void run()
{
while(true)
{
for(position=0;position) {
repaint();
try
{mythread.sleep(100);
}
catch(InterruptedException e)
{
}
}
}
}
public void stop()
{
mythread.stop();
mythread = null;
}
public void paint(Graphics g)
{
g.setColor(Color.gray);
g.fillOval(position,50,30,30);
g.setColor(Color.black);
g.fillOval(position+6,58,5,5);
g.fillOval(position+20,58,5,5);
g.drawLine(position+15,58, position+15,68);
g.drawLine(position+12,68, position+15,68);
g.drawLine(position,45,30,30,-50,-70);
}
}

Popularity: 3% [?]

CASE Function in SQL Server 2005

Sql Case Function :

  • The CASE function allows you to evaluate a column value on a row against multiple criteria, where each criterion might return a different value
  • CASE is just a searched (or lookup) expression – you cannot RETURN from inside it – it’s kind of like IF() in Excel


Sql Case Function has two Types.

  • Simple Function
  • Searched Function

Simple Function :

Syntax :

case @Type
when expression then Result
when expression then Result
else Result

Example for Simple Function:

Set @state=’IND’

Select
state,
case @state
when ‘IND’ then ‘INDIA’
when ‘PAK’ then ‘Pakistan’
when ‘RSA’ then ‘South Africa’
when ‘Lanka’ then ‘Sri Lanka’
end as State Name
from State_name

Result:

State State_name
==== =========
IND INDIA

Searched Function :

Syntax :

case
when boolean_expression then Result
when boolean_expression then Result
else Result

Example for Searched Function:

Table :
Code State
=== ====
1 I
2 IND
3 PAK
4 RSA
5 SL

Select
state,
case
when state in (‘IND’,'I’) then ‘INDIA’
when state in (‘PAK’,'P’) then ‘Pakistan’
when state in (‘RSA’,'SA’) then ‘South Africa’
when state in (‘Lanka’,'SL’) then ‘Sri Lanka’
end as State Name
from State_name

Result :

State State Name
==== =========
I INDIA
IND INDIA
PAK Pakistan
RSA South Africa
SL Sri Lanka

Popularity: 46% [?]

Copy-Paste to and from clipboard using c#

To make the operations such as copy paste using c#,we have to use the Clipboard Class which provides methods to place data on and retrieve data from the system Clipboard.

Example :-

Copy the text from txtCopy to clipboard

 System.Windows.Forms.Clipboard.SetDataObject(txtCopy.Text, true);

Paste the text from clipboard to txtPaste

IDataObject clipData = Clipboard.GetDataObject();
if (clipData.GetDataPresent(DataFormats.Text))
{
txtPaste.Text = clipData.GetData(DataFormats.Text).ToString();
}
else
{
txtPaste.Text = "Could not retrieve data from the clipboard.";
}

Download

Popularity: 23% [?]

Get Clients IP Address Using ASP.NET

We can get the clients IP address using the Request object’s(The Request object retrieves the values that the client browser passed to the server during an HTTP request. ) property Request.ServerVariables Collection.
Request.ServerVariables Collection gets the values of predetermined environment variables.

“REMOTE_ADDR” gets the IP address of the remote host making the request. 

string ipaddr;

ipaddr = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

here the string “ipaddr” stores the clients ip.

List Of other Request.ServerVariables Object

Variable Description
AUTH_TYPE The authentication method that the server uses to validate users when they attempt to access a protected script.
CONTENT_LENGTH The length of the content as given by the client.
CONTENT_TYPE The data type of the content. Used with queries that have attached information, such as the HTTP queries POST and PUT.
GATEWAY_INTERFACE The revision of the CGI specification used by the server. Format: CGI/revision.

HTTP_<HeaderName> The value stored in the header HeaderName. Any header other than those
listed in this table must be prefixed by “HTTP_” in order for the
ServerVariables collection to retrieve its value.

Note: The server interprets any underscore (_) characters in
HeaderName as dashes in the actual header. For example if you specify
HTTP_MY_HEADER, the server searches for a header sent as MY-HEADER.

LOGON_USER The Windows NT® account that the user is logged into.
PATH_INFO Extra path information as given by the client. You can access scripts
by using their virtual path and the PATH_INFO server variable. If this
information comes from a URL it is decoded by the server before it is
passed to the CGI script.
PATH_TRANSLATED A translated version of PATH_INFO that takes the path and performs any necessary virtual-to-physical mapping.
QUERY_STRING Query information stored in the string following the question mark (?) in the HTTP request.
REMOTE_ADDR The IP address of the remote host making the request.
REMOTE_HOST The name of the host making the request. If the server does not have
this information, it will set REMOTE_ADDR and leave this empty.
REQUEST_METHOD The method used to make the request. For HTTP, this is GET, HEAD, POST, and so on.
SCRIPT_MAP Gives the base portion of the URL.
SCRIPT_NAME A virtual path to the script being executed. This is used for self-referencing URLs.
SERVER_NAME The server’s host name, DNS alias, or IP address as it would appear in self-referencing URLs.
SERVER_PORT The port number to which the request was sent.
SERVER_PORT_SECURE A string that contains either 0 or 1. If the request is being handled
on the secure port, then this will be 1. Otherwise, it will be 0.
SERVER_PROTOCOL The name and revision of the request information protocol. Format: protocol/revision.
SERVER_SOFTWARE The name and version of the server software answering the request (and running the gateway). Format: name/version.
URL Gives the base portion of the URL.

Popularity: 31% [?]

Convert Text to Image using c#

We can Convert text to image in c# using System.Drawing namespace.
Here we create a bitmap image and use its Graphics property to convert text passed to image and place the image in a picture box.

private void TextToImage()
{
Color BackColor = Color.White;
String FontName = "Times New Roman";
int FontSize = 25;
int Height = 50;
int Width = 300;
<span style="color: rgb(56, 118, 29);">
//creating bitmap image
<span style="color: rgb(0, 0, 0);">Bitmap </span></span>bitmap = new Bitmap(Width, Height);
 
<span style="color: rgb(56, 118, 29);">//FromImage method creates a new Graphics from the specified Image.</span>
Graphics graphics = Graphics.FromImage(bitmap);
Color color = Color.Gray;
Font font = new Font(FontName, FontSize); 
 
SolidBrush BrushBackColor = new SolidBrush(BackColor);
Pen BorderPen = new Pen(color);
 
Rectangle displayRectangle = new Rectangle(new Point(0, 0), new Size(Width - 1, Height - 1));
<span style="color: rgb(56, 118, 29);">//Drawing a rectangle and filling the background as white
<span style="color: rgb(0, 0, 0);">graphics</span></span>.FillRectangle(BrushBackColor, displayRectangle);
graphics.DrawRectangle(BorderPen, displayRectangle); <span style="color: rgb(56, 118, 29);">
 
//giving the font and color to the string passed</span>
graphics.DrawString(txtEnter.Text,font,Brushes.Red, 0, 0);
pictureBox1.Image = bitmap;
 
}

Download

Popularity: 15% [?]

JavaBeans

Requirements for JavaBeans:

  • there have to be set/get methods specified
  • has to have public default constructor (without parameters)
  • it should be in a package
  • serializable
  • get/set methods has to have the same data type!
  • bean cannot modify content of a page!

… in general, JavaBean is a standard Class.

It can be used as a simple component in a JSP page.

- get method has to be fast, and it should be plain and simple.

usage in a JSP page:

<jsp:useBean id=”nameOfBean” scope=”session” class=”myPackage.Counter”>

<jsp:setProperty name=”nameOfBean” property=”myCount”>

this is the same as

<%= Counter.myCount %>

with JSTL Expression language:

<c:out value=”${Counter.myCount}”>

automatically set the bean attributes from names (!important!):
<jsp:setProperty name=”pocetMiest” property=”*”>

Scope can be:

  • application - the bean is available in all application, when that’s in more JSP pages, one of them can create it once and other JSP pages can use it. It’s something like a global object for all application.
  • session - stored data, just for this session
  • request - just for request or forward
  • page - smallest scope, just on a page that is called (forwarded or included)

Popularity: 1% [?]

Get available server names of Sql Server within the local network – SqlDataSourceEnumerator

SqlDataSourceEnumerator is a class in .net framework that gets the available instances of SQL Server within the local network.

DataTable dt = SqlDataSourceEnumerator.Instance.GetDataSources();

SqlDataSourceEnumerator.Instance.GetDataSources() returns a datatable with the available servers in the current network.

The DataTable returned as 4 colunms such as :-

  • ServerName
  • InstanceName
  • IsClustered
  • Version

Popularity: 1% [?]

Sorting Program using C#

namespace SortingNos
{
   class Program
   {
       static void Main(string[] args)
       {
           Console.WriteLine("Enter Numbers :");
           int[] arr = new int[5];
           string arr1 = "";
           for (int i = 0; i < 5; i++)
 {
 arr1 = Console.ReadLine();
 arr[i] = Convert.ToInt32(arr1);            
}
 
 int a, b, temp = 0;
 Console.WriteLine();
 for (a = 0; a < arr.Length; a++)
           {
                               for(b=a+1;b< arr.Length;b++)
               {
                   if (arr[a] > arr[b])
                   {
                       temp = arr[a];
                       arr[a] = arr[b];
                       arr[b] = temp;
                   }
               }             
               Console.WriteLine(arr[a]);              
           }          
          Console.ReadLine();
       }
   }
}

Download

Popularity: 5% [?]

Designed by: Business Web Hosting | Thanks to Buy Icons, travel tips and Used Cars