Java-SQL Server Database Connectivity

In order to establish a database connection between Java Swing and SQL Server, we have to create a Data Source Name from Control Panel-->Administrative Tool-->Data Source(ODBC). Here in following example, I have used a Data Source named "SQLDSNSource".
In this example, I have tried to navigate the records available in a SQL table, adding new record, updating record and deleting record.
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class DBConnectivity extends JFrame implements ActionListener
{
Container container;

JLabel lblRoll=new JLabel("Roll");
JLabel lblName=new JLabel("Students Name");
JLabel lblAddress=new JLabel("Address");
JLabel lblBatch=new JLabel("Batch");

JButton btnNext=new JButton("Next >>");
JButton btnPrior=new JButton("<< Previous");

JTextField txtRoll=new JTextField(8);
JTextField txtName=new JTextField(20);
JTextArea txtAddress=new JTextArea(3,20);
JTextField txtBatch=new JTextField(10);

JPanel p1=new JPanel();
JPanel p2=new JPanel();

JButton btnNew=new JButton("New Record");
JButton btnSave=new JButton("Save Record");
JButton btnUpdate=new JButton("Update");
JButton btnDelete=new JButton("Delete");

ResultSet rs;
Connection con;
Statement st;

DBConnectivity(String title)
{
super(title);
container=getContentPane();
container.setLayout(new GridLayout(6,2));

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbcdbc:SQLDSNSource");
st=con.createStatement(rs.TYPE_SCROLL_SENSITIVE, rs.CONCUR_UPDATABLE);
rs=st.executeQuery("select * from student");

rs.next();

txtRoll.setText(rs.getString("Roll"));
txtName.setText(rs.getString("Name"));
txtAddress.setText(rs.getString("Address"));
txtBatch.setText(rs.getString("Batch"));
}
catch(Exception e)
{
}

txtRoll.setEnabled(false);
btnSave.setEnabled(false);
container.add(lblRoll);
container.add(txtRoll);
container.add(lblName);
container.add(txtName);
container.add(lblAddress);
container.add(txtAddress);
container.add(lblBatch);
container.add(txtBatch);

p1.add(btnNew);
p1.add(btnSave);
p2.add(btnUpdate);
p2.add(btnDelete);

btnPrior.addActionListener(this);
btnNext.addActionListener(this);
btnNew.addActionListener(this);
btnSave.addActionListener(this);
btnUpdate.addActionListener(this);
btnDelete.addActionListener(this);

container.add(btnPrior);
container.add(btnNext);
container.add(p1);
container.add(p2);

pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e)
{
try
{
if (e.getSource()==btnNext)
{
rs.next();
}

if (e.getSource()==btnPrior)
{
rs.previous();
}

if (e.getSource()==btnNew)
{
txtRoll.setText("");
txtRoll.setEnabled(true);
txtName.setText("");
txtAddress.setText("");
txtBatch.setText("");
btnNew.setEnabled(false);
btnSave.setEnabled(true);
btnUpdate.setEnabled(false);
btnDelete.setEnabled(false);
}

if (e.getSource()==btnSave)
{
String saveCom="insert into student values('"+txtRoll.getText()+"','"+txtName.getText( )+"','"+txtAddress.getText()+"','"+txtBatch.getTex t()+"')";
st.executeUpdate(saveCom);
JOptionPane.showMessageDialog(this, "New Record has been saved");
txtRoll.setEnabled(false);
btnNew.setEnabled(true);
btnSave.setEnabled(false);
btnUpdate.setEnabled(true);
btnDelete.setEnabled(true);
}

if (e.getSource()==btnUpdate)
{
String updateCom="update student set name='"+txtName.getText()+"',address='"+txtAddress .getText()+"',batch='"+txtBatch.getText()+"' where roll='"+txtRoll.getText()+"'";
st.executeUpdate(updateCom);
JOptionPane.showMessageDialog(this, "Record has been updated");
st.close();
}

if (e.getSource()==btnDelete)
{
String deleteCom="delete from student where roll='"+txtRoll.getText()+"'";
st.executeUpdate(deleteCom);
JOptionPane.showMessageDialog(this, "Record has been deleted");
st.close();
}

txtRoll.setText(rs.getString("Roll"));
txtName.setText(rs.getString("Name"));
txtAddress.setText(rs.getString("Address"));
txtBatch.setText(rs.getString("Batch"));
}
catch(Exception ex)
{
System.out.println(ex);
}
}

public static void main(String args[])
{
new DBConnectivity("Database Connectivity");
}
}


Posted by Shafiq at 3:56 AM 0 comments Links to this post
Labels: Java-SQL Connectivity


Sunday, August 24, 2008

Creating Help File in Visual Basic 6.0


We can create Help File in MS Word as well as in HTML. Here I would like to describe how to create a Help File for VB using MS Word.

First of all open a new document in MS Word. You can give a title for this document. Create some necessary topic on which you want to provide help to the client. Put any ID numbers along with the help topics without keeping any space. Make all the topics double underlined and hide the IDs.

Give a page break and write the description of help topic 1 and give another page break and write the help description for topic 2 and so on for all help topics.


Keep the mouse cursor at the beginning of the topic description and from the insert menu select reference and add footnote for all the topics description one by one and the footnote number should be the same as the ID number that we have given along with the topic name.
The custom mark for the footnote should be # symbol.

The MS Word page will look like this.


Put a reference for the first topic also.
Save the document in a Rich Text Format.

You should have Microsoft Help Workshop installed in your machine. If you don’t have it, just download hcw.exe from internet and install the same.

Open start --> Microsoft Help Workshop --> Help Workshop

File --> New --> Help Project --> OK --> Enter a Project Name (for a new help project) --> OK.


Click on Files --> Add --> Browse the location of Rich Text File that we have saved before.

Click on Map --> Enter the Topic Id (we have used in .rtf file) and put Numeric value for the same that we will use in future.




Now click on “Save and Compile” button.

Our Help File has been created where we had saved the Help Project.

We need to copy the Help file from that location and paste into the VB Project location in which Project we want to add this Help File.

In order to make this Help file available in our visual basic project, we can go to Project menu and Project Properties. Then browse the location of help file and put the numeric id of the main topic --> OK.

Run the project and press F1 to invoke the help window.
Posted by Shafiq at 6:48 AM 0 comments Links to this post
Labels: Help File in VB 6.0



Generating Crystal Report in Visual Basic 6.0

We can create crystal report in Visual Basic 6.0. Sometimes we generate the report based on parameter entered by the user and sometimes we generate the report without any input of user. If we need to pass parameter, it is better to create stored procedure. If we don’t need to pass parameter, we can directly display the required data from one or more than one table. We want to create a report, which will display a student’s detail based on the roll number entered by the end-user as a parameter. The procedure to create a report is given below-
In Project Explorer Window, Right Click on Designers --> Add --> Data Environment.
You will get the data environment window with a new connection option listed in it. Right click on the connection name -->properties -->select appropriate provider to connect to database, select “Microsoft OLEDB Provider for SQL Server” in order to connect to SQL Server --> Next -->Select Server Name --> Authentication(user id, password if required) --> select the database from where you want to display the data -->OK.
Then again right click on Connection name and select Add Command submenu.
Right Click on the Command Name -->Properties.
Here we can write the SQL Statement directly or we can select a stored procedure from Database Object drop down list.
We will select the Parameters Tab to check whether the data type of the parameter matches with the data type of the field in database or not. Here in my database the Roll Number has been declared as Varchar so the parameter is adVarChar in report.
Grouping and Aggregate Tabs are necessary in case we need to perform any calculation using group by keyword. Finally we press on OK button.
At this stage, the Data Environment window will look like follows-
Now we will add a Data Report from the Project Explorer by right clicking on the Designer folder.
We need to set 2 directives from the properties of Data Report, i.e. Data Source = DataEnvironment1 and DataMember = Command1.
Then right click on the body of the Data Report and select Retrieve Structure.
Finally we will drag the necessary field from the Data Environment window and drop in Data Report window. Arrange the fields to make better look. We can add some additional fields also to make the report attractive.
In order to pass the Roll Number as a parameter, we can create a simple window like below-
And on click of View Report button, we can write the following code-

Private Sub cmdViewReport_Click ()

DataEnvironment1.Command1 txtRoll.Text

DataReport1.Show

End Sub
The Crystal Report of Visual Basic 6.0 is ready to use.


Posted by Shafiq at 12:10 AM 0 comments Links to this post
Labels: Crystal Report in VB 6.0



Monday, August 18, 2008

Visual Basic 6.0 and SQL Server Connectivity

In order to develop easy, user friendly but reliable software, we can use Visual Basic 6.0 and SQL Server. Visual Basic provides us with a nice IDE to create user interface very easily. The only complex thing is database connectivity. Here I would like to show you the connectivity between VB and SQL Server.
First of all, it is better to create a module from the Project Explorer window. And in the General Declaration of that module we need to declare 3 objects i.e. RecordSet, Connection and Command. In order to access these 3 objects we need to add the reference of Microsoft ActiveX Data Object 2.1 or any other version from Project à Reference menu.
And we can create a Procedure in it, in order to reuse the procedure from every form where we need the database connectivity. So this procedure will reduce the size of our code. After writing all necessary objects and procedure the module will look like below-

Public rs As Recordset
Public con As Connection
Public com As Command
_____________________________________
Public Sub prcConnect ()
Set rs = New Recordset
Set con = New Connection
Set com = New Command
With con
. ConnectionString = "data source= shafiq; initial catalog= studentadmin; user id= sa; password=1234;" [for SQL Server Authentication]
. ConnectionString = "data source= shafiq; initial catalog= studentadmin; integrated security=SSPI" [for Windows NT authentication]
.Provider = "SQLOLEDB"
.Open
End With
com.ActiveConnection = con
End Sub


Then open a new form and create the necessary interface to display the required field from the database. And on form load event, write the following codes-

Private Sub Form_ Load ()
prcConnect
rs.Open "select * from student", con, adOpenDynamic, adLockOptimistic
rs.MoveFirst
While Not rs.EOF
Combo1.AddItem rs.Fields ("Roll") & ""
rs.MoveNext
Wend
rs.MoveFirst
prcDisplay
' App.HelpFile = MYHELPPROJECT.hlp
txtRoll.Visible = False
End Sub


Here is the code of prcDisplay () function-

Public Sub prcDisplay ()
Combo1.Text = rs.Fields ("Roll") & ""
txtName.Text = rs.Fields ("Name") & ""
txtAddress.Text = rs.Fields ("Address") & ""
txtBatch.Text = rs.Fields ("Batch") & ""
End Sub
There are several ways to navigate between the records of the database i.e. we can use buttons or combo box etc. I have used here a combo box, so on choose of a roll number from the combo box, the relevant record of that particular roll number should be displayed. Here is the code on click of combo box event to do so-

Private Sub Combo1_ Click()
rs.MoveFirst
Dim check As Integer
check = Combo1.ListIndex
rs.Move check
prcDisplay
End Sub
For adding a new record into the database we can write the following code on click of a button-

Private Sub cmdNew_ Click ()
Combo1.Visible = False
TxtRoll.Visible = True
cmdUpdate.Visible = False
cmdSave.Visible = True
For Each Control In frmStudent
If TypeOf Control Is TextBox Then
Control.Text = ""
End If
Next
Rs.MoveLast
rs.AddNew
txtRoll.SetFocus
End Sub
For saving a new record in database, we can write the following code-

Private Sub cmdSave_ Click ()
rs.Fields ( "Roll") = txtRoll.Text & ""
rs.Fields ( "Name") = txtName.Text & ""
rs.Fields ( "Address") = txtAddress.Text & ""
rs.Fields ( "Batch") = txtBatch.Text & ""
rs.update
MsgBox "Saved Successfully", vbInformation, "Saved"
Combo1.Visible = True
Combo1.AddItem txtRoll.Text & ""
txtRoll.Visible = False
rs.Requery
Combo1.Refresh
cmdUpdate.Visible = True
cmdSave.Visible = False
End Sub
Updating data in database is depended on data type of the field. For updating the record in database, we can write the following code-

Private Sub cmdUpdate_ Click ()
If txtName.Text = "" Then
rs.Fields ( "Name") = Null
Else
rs.Fields ( "Name") = txtName.Text
End If
If txtAddress.Text = "" Then
rs.Fields ( "Address") = Null
Else
rs.Fields ( "Address") = txtAddress.Text
End If
If txtBatch.Text = "" Then
rs.Fields ( "Batch") = Null
Else
rs.Fields ( "Batch") = txtBatch.Text
End If
rs.update
MsgBox "Saved Successfully", vbInformation, "Saved"
End Sub


OR we can write like follows-

Private Sub cmdUpdate_ Click ()
On Error GoTo err_q
rs.Fields ( "Name") = txtName.Text & ""
rs.Fields ( "Address") = txtAddress.Text & ""
rs.Fields ( "Batch") = txtBatch.Text & ""
rs.update MsgBox "Saved Successfully", vbInformation, "Students Detail"
Exit Sub
err_q :
MsgBox "Could not connect to the server", vbCritical, "Student Admin"
End Sub
For deleting a record from the database, we can write the following code-

Private Sub cmdDelete_ Click ()
com.CommandText = "delete from student where roll='" & Combo1 & "'"
com.Execute MsgBox "Record has been deleted ", vbInformation, "Delete Record"
Combo1.RemoveItem Combo1.ListIndex
rs.MoveFirst
rs.Requery
Combo1.Refresh
prcDisplay
End Sub


Posted by Shafiq at 6:43 AM 0 comments Links to this post
Labels: VB 6.0 and SQL Connectivity



Monday, March 10, 2008

How to Install & Configure Apache, PHP, MySQL and Perl on Windows


It is very tedious process to install and configure Open Source Technologies like PHP, Apache, MySQL and Perl on Windows. A few settings need to be done to work with each other especially if we want to enable PHP, MySQL and Perl on Apache server. Of course, if someone wants to work with PHP only, he can make use of IIS also. Since PHP needs to be connected to MySQL to display data to website from database, so we need to learn how to make all applications connected to each other. Here I have enlisted the procedures step by step to make the following tasks very easy-
  • Apache Installation
  • Apache Configuration
    • Enabling SSI with Apache
    • Enabling PHP with Apache
    • Enabling Perl with Apache
    • Enabling CGI scripts with Apache
  • PHP Installation
    • PHP Configuration
    • Enabling MySQL with PHP
    • Enabling email functionality with PHP
  • MySQL Installation
  • PHP and MySQL Database Connectivity
  • Perl Installation
There are different packages for different Operating Systems. So you should download suitable software according to your Operating System.
There is no sequence to install these software, you can install anyone first. But I prefer to install Apache first. So let us start from Apache installation.

Apache Installation: (Apache_2.2.4-win32-x86-no_ssl)
So many sites are there that offer free apache download. You can download any updated version of apache from any website. Here I have downloaded apache_2.2.4-win32-x86-no_ssl. This is .msi file, so just click on it and the installation will start. While installing apache, keep all the default settings except following—
  • ServerName : localhost
  • Network Domain : localhost
  • Admin email : Put any email id you want.
  • Setup Type : Typical
Restart the machine and check whether apache service has been started or not. The apache icon should appear on the taskbar.

Configuring Apache
Apache has got a dynamic configuration file named “httpd.conf”. Whatever changes we make here reflects to other applications after restarting the apache service. To open the httpd.conf file we can go to Start-> Programs-> Apache HTTP Server->
Configure Apache Server-> Edit the Apache httpd.conf Configuration File. Or we can open it manually from the location: "C:\Program Files\Apache Software Foundation\Apache\conf\httpd.conf".
We will make some changes into the httpd.conf configuration file after installing every software to make
them enabled with Apache. Let us make some basic changes into httpd.conf file now.
To Enable Server Side Includes (SSI), find out following lines and uncomment them by removing # sign. Apache uses # sign to comments a line.
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
To make .shtml file the default index page in apache, write following line—

DirectoryIndex index.shtml index.html [I will explain this later on]
Close the httpd.conf file and restart the Apache Service.

PHP Installation (PHP-5.2.3-Win32)
In case of PHP, it is better to download the zip package from the "Windows Binaries" section instead of downloading installer as because in Windows Binaries we get some .dll files, which are very much necessary for configuration. Here I have downloaded PHP
5.2.3 zip package. You can install any other updated version.
It is completely different to install Windows Binary. Under windows binary package, there is no setup file.
So simply we need to extract the .zip file to the location wherever we want. Let’s say, we want to install PHP to "C:/PHP" location. So just
create a folder called PHP in C: drive and extract all the files from the zip package into that folder.
PHP installation is done!
Important: if you had installed PHP earlier and you want to upgrade it now, ensure that there is no old PHP.INI file existed in C:/Windows folder before installing the updated version of PHP.



Configuring PHP
There is a file namely "php.ini-recommended" in C:/PHP folder, rename it into “php.ini”. You need not to copy this file to C:/Windows now because we will write a directive in httpd.conf
file later on to enable PHP with Apache.
Let us open php.ini file in order to make some changes based on our requirements.
We know that PHP script starts with “< ? php”. So we need to enable “< ?” sign, find the following line to do same—
short_open_tag = On
While testing and debugging a PHP script, it is necessary to display the appropriate errors so that we can understand the mistakes and accordingly we can resolve it. But for a live website, it is better not to display errors.
We can enable or disable displaying error by the following directive—
display_errors = On
If you want to use mail() function to send mail successfully, first of all you have to be connected with ISP and in php.ini file you need to change your SMTP Server(name or IP address) and email account like follows—

[mail function]
SMTP = mail.alzouman.com.sa
smtp_port = 25
sendmail_from = shafiq@alzouman.com.sa

[suppose my server name is : mail.alzouman.com.sa and email id is: shafiq@alzouman.com.sa]

Enabling PHP with Apache
Now we can enable PHP script to run on Apache Server. There is a .dll file in C:/PHP/ location named php5apache2_2.dll that we need to add in httpd.conf file. You will find several .dll files there and their names are specified based on PHP version number and Apache version number. Since our PHP version number is 5.2.3 and Apache version number is 2.2.4, php5apache2_2.dll is appropriate for us to establish a bridge between PHP and Apache.
Open the httpd.conf file and look for LoadModule statement. Add following line under all LoadModule statements-
LoadModule php5_module “c:/php/php5apache2_2.dll”
Find out “AddType” statement inside < IfModule mime_module> section and add following line before < / IfModule> statement.
AddType application/x-httpd-php .php
If you need to support other file types like .phtml, simply add them to the list like below-

AddType application/x-httpd-php .phtml
Finally to indicate the location of php.ini file, add following line-
PHPIniDir “C:/PHP”



MySQL Installation (mysql-essential-4.1.11-win32)
It is very easy to install MySQL. Download mysql-essential-4.1.11-win32 or any other version.
Run the .msi file and choose the following options while installing MySQL—
  • Typical Setup
  • Skip Sign-Up
  • Configure the MySQL Server now
  • Detailed Configuration
  • Developer Machine
  • Multifunctional Database
  • InnoDB Tablespace Settings
  • Decision Support (DSS)/OLAP
  • Enable TCP/IP Networking
  • port number at 3306
  • Standard Character Set
  • Install As Windows Service
  • Enter your root password
  • Enable root access from remote machines
  • Execute

Enabling MySQL with PHP
We need to establish a connection between PHP and MySQL in order to work with database. Open the php.ini file once again. Find out the following line and remove comments ( from the beginning of the line.
;extension=php_mysql.dll

Save the modified php.ini file.
Copy C:/PHP/libmysql.dll and C:/PHP/ext/php_mysql.dll to the C:/Windows/system32 folder.
Restart Apache. In order to check whether PHP and MySQL are working with Apache or not we can write a simple PHP script like follows—
<?php
phpinfo();
?>
Save this file in C:\Program Files\Apache Software Foundation\Apache\htdocs\ location with “anyname.php” extension. Open Internet Explorer or any other browser and type http://localhost/anyname.php it should display mysql modules. If it doesn’t display, ensure that php_mysql.dll is copied to system32 folder.

PHP-MySQL Connection test
<?php
$server='localhost';
$user='root';
$pass='1234';
$dbName='mysql';
$con = mysql_connect("$server","$user", "$pass");
if($con)
{
print "Connected successfully";
}
else
{
die("Unable to connect");
}
$selectdb= mysql_select_db("$dbName") or die("Could not select database");
$sqlquery= ("select * from user;");
$result=mysql_query($sqlquery);
if(mysql_num_rows($result)<1)
{
echo "No result";
}
else
{
while($row=mysql_fetch_array($result))
{
echo "<br><br>$row[Host]";
echo "&nbsp; $row[User]";
echo "&nbsp; $row[Password]";
}
}
mysql_close($con);
?>

Perl Installation (ActivePerl-5.10.0.1002-MSWin32-x86-283697)
I have downloaded ActivePerl-5.10.0.1002-MSWin32-x86-283697. You may download any updated Perl as you like. Installing Perl is as simple as other application’s installation just by pressing next button with the default values and finish.

Enabling Perl & CGI Scripts with Apache
We need to configure Apache to make the perl actively working with Apache Web Server. Let us look into the steps to run Perl scripts and CGI scripts.
Open the Apache configuration file httpd.conf again. There is a specific folder for CGI script that is: "C:/Program Files/Apache Software Foundation/Apache/cgi-bin/". We can save our CGI script here as well as we can run from other location also. To enable this folder to run CGI script, find out “ScriptAlias” statement in httpd.conf file and uncomment following line by removing # sign—
ScriptAlias /cgi-bin/ "C:/Program Files/Apache Software Foundation/Apache/cgi-bin/"
To run CGI script anywhere in your domain, add the following line—
AddHandler cgi-script .cgi
If you want to recognize .pl extension as a CGI script, you can add .pl extension along with .cgi like follows—
AddHandler cgi-script .cgi .pl
We should have the permission to run any CGI script. To invoke the execution permission to CGI script, look for the following block and make the changes like follows—
<Directory/>
Options FollowSymLinks +ExecCGI
AllowOverride None
Order deny,allow
Deny from all
Satisfy all
</Directory>
And—
Options Indexes FollowSymLinks +ExecCGI
Apache Server can contain so many types of file for example, .cgi, .pl, .html, .phtml, .shtml etc. The default index page, which is written under DirectoryIndex directives displays when we open http://localhost/ from browser. If multiple index pages are mentioned there, it looks for first index page, in case of unavailability of first index page it looks for second and so forth. So according to your expectation, you can enlist the index pages based on the priority you want—
DirectoryIndex index.html index.php index.cgi index.pl index.shtml
Restart the Apache Server and That’s All.


Posted by Shafiq at 7:26 AM 0 comments Links to this post
Labels: OST Installation and Configuration



Insert Data into MySQL from PHP

dbCon.html

<html>
<head>
<title>Student Registration</title>
</head>

<body>
<div align="center">
<h2><font color="#000099" face="Verdana, Arial, Helvetica,
sans-serif">Student Registration</font></h2>
<form name="form1" method="get" action="dbCon.php">
<p>&nbsp;</p>
<table width="52%" height="200" border="1"
align="center" cellpadding="5" cellspacing="5"
bordercolor="#000000">
<tr>
<td width="31%"><font color="#000099">Roll</font></td>
<td width="69%"><input name="roll" type="text"
size="20"></td>
</tr>
<tr>
<td><font color="#000099">Name</font></td>
<td><input name="nm" type="text" size="50"></td>
</tr>
<tr>
<td><font color="#000099">Address</font></td>
<td><textarea name="address" cols="30"></textarea></td>
</tr>
<tr>
<td><font color="#000099">Batch</font></td>
<td><input name="batch" type="text" size="20"></td>
</tr>
<tr bordercolor="#000000">
<td colspan="2"><div align="center">
<input type="submit" name="Submit" value="Submit">
<input name="Reset" type="submit" id="Reset"
value="Reset">
</div></td>
</tr>
</table>
<p>&nbsp;</p>
</form>
</div>
</body>
</html>



dbCon.php

<?php
$server='localhost';
$user='root';
$pass='1234';
$dbName='Student_Admin';
$con = mysql_connect("$server", "$user", "$pass");

echo "<h2 align=center>
PHP-MySQL Connectivity<br></h2><hr>";

if($con)
{
print "Connected successfully<br>";
}
else
{
die("Unable to connect");
}

$selectdb= mysql_select_db("$dbName")
or die("Could not select database");

$roll=$_GET['roll'];
$nm=$_GET['nm'];
$address=$_GET['address'];
$batch=$_GET['batch'];

$sql_insert="insert into Student values('$roll','$nm','$address','$batch');";
$result=mysql_query($sql_insert);
if($result)
{
echo "The record is inserted";
}
else
{
echo "unable to insert record";
mysql_error();
}

echo "<br><hr><h3 align=center><br>Display Data from Database <br></h3>";
$sqlquery= ("select * from Student;");
$result=mysql_query($sqlquery);

if(mysql_num_rows($result)<1)
{
echo "No result";
}
else
{
echo "<br><table align=center width=70% border=1 bordercolor=#0000FF
cellpadding=5 cellspacing=5>";
echo "<th>Roll</th><th>Name</th><th>Address</th><th>Batch</th>";
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>$row[Roll]</td>";
echo "<td>$row[Name]</td>";
echo "<td>$row[Address]</td>";
echo "<td>$row[Batch]</td>";
echo "</tr>";
}
echo "</table>";
}

mysql_close($con);
?>