0% found this document useful (0 votes)
84 views10 pages

Java Lab Programs - 3

This Java program demonstrates inserting data into an MS Access database table called "jstudent" with two columns "rollno" and "s_name" using JDBC. It takes the roll number and name as command line arguments, prepares an INSERT statement, executes it, and prints a message on success. A second program displays all rows from the "jstudent" table by preparing a SELECT statement, iterating through the result set and printing the column values.

Uploaded by

Lady Bug
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views10 pages

Java Lab Programs - 3

This Java program demonstrates inserting data into an MS Access database table called "jstudent" with two columns "rollno" and "s_name" using JDBC. It takes the roll number and name as command line arguments, prepares an INSERT statement, executes it, and prints a message on success. A second program displays all rows from the "jstudent" table by preparing a SELECT statement, iterating through the result set and printing the column values.

Uploaded by

Lady Bug
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Q19. Write a Java program to pass parameter to an applet .

import java.awt.*;
import java.applet.*;
/*<applet code=helloparam.class height=300 width=250>
<param name="string" value="Applet!!!!!">
</applet>*/
public class helloparam extends Applet
{
String str;
public void init()
{
str=getParameter("string");
if(str==null)
str="5BCA";
str="hello"+" "+str;
}
public void paint(Graphics g)
{
g.drawString(str,10,100);
}
}
To run the applet application:
C:\java program>javac helloparam.java
C:\java program>appletviewer helloparam.java

If value=”Applet!!!!!” If value attribute is not given in the applet tag


Q20. Create a Java applet application to check whether a given number is even or
not .
import java.awt.*;
import java.applet.*;
/* <applet code=displayeven.class width=300 height=200></applet>*/
public class displayeven extends Applet
{
TextField text1;
public void init()
{
text1=new TextField(8);
add(text1);
text1.setText("0");
}
public void paint(Graphics g)
{
int x=0,y=0;
String s1,s;
g.drawString("Input one numbers in textbox",10,50);
s1=text1.getText();
x=Integer.parseInt(s1);

y=x%2;
if(y==0)
s=x+" is Even number";
else
s=x+" is Odd number";
g.drawString(s,10,75);
}
public Boolean abc(Event event,Object object)
{
repaint();
return true;
}
}
Output: To run the program:
F:\java pro\java\applet>javac displayeven.java
F:\java pro\java\applet>appletviewer displayeven.java
Q21. Create a Java applet application to demonstrate arithmetic operation using
Event Handling.
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
/*<applet code="readapplet.class" width=300 height=200></applet>*/
public class readapplet extends Applet implements ActionListener
{ Label l1, l2, l3;
intx,y,z;
TextField t1, t2,t3;
Button b1;
public void init()
{
setLayout(new GridLayout(4, 2));
t1 =new TextField(8);
t2 = new TextField(8);
t3 = new TextField(8);
b1=new Button("Calculate");
l1 = new Label("Number 1");
l2 = new Label("Number 2");
l3 = new Label("Result");
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b1);

t1.setText("0");
t2.setText("0");
t3.setText("0");
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == b1)
{
x = Integer.parseInt(t1.getText());
y = Integer.parseInt(t2.getText());
z = x + y;
t3.setText(String.valueOf(z));
}
}
}
To run the program:
F:\java pro\java\applet\New Folder>javac readapplet.java
F:\java pro\java\applet\New Folder>appletviewer readapplet.java
Q22. Create a Java applet application to demonstrate free hand drawing on applet.

import java.applet.*;
import java.awt.*;
public class freehand extends Applet
{
private intlast_x=0;
private intlast_y=0;
public booleanmouseDown(Event e, int x, int y)
{
last_x=x;
last_y=y;
return true;
}
public booleanmouseDrag(Event e, int x, int y)
{
Graphics g=getGraphics();
g.drawLine(last_x, last_y, x, y);
last_x=x;
last_y=y;
return true;
}
}
/* <applet code=freehand width=300 height=300></applet>*/
To run the program:
F:\java pro\java\applet>javac freehand.java
Note: freehand.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

F:\java pro\java\applet>appletviewer freehand.java


Q23. Write a Java program to demonstrate insert operation on the MS-Access
database.

Step 1: Create the MS-Access Database with table name as: “jstudent”
Column_Name Datatype
rollno Number
s_name Text

Step 2: Create the DSN in the Control Panel as follows:


Go to Administrative Tools -> ODBC Data Sources(32 bit)

Here click on Add button. Following window will appear.


Here select the driver. As we are using MS-Access database , we select ” Microsoft Access
Driver(*.mdb,*.accdb) “. Click on “Finish”. Following window will appear.

jdsnstud

In this window , enter the Data Source Name( in this case it is “jdsnstud”) .

To select the database, Click on Select button that opens the following window and there you have
to select the database. And Click Ok button.

After this step , your DSN is created.


Step 3: Enter the following code

import java.sql.*;

public class jinsert


{
public static void main(String args[])
{
PreparedStatement ps;
Connection con=null;
String str1;
int rno;
rno = Integer.parseInt(args[0]);
str1 = args[1];
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con= DriverManager.getConnection("jdbc:odbc:jdsnstud");
ps = con.prepareStatement("insert into jstudent values(?,?)");
ps.setInt(1,rno);
ps.setString(2,str1);

ps.executeUpdate();
con.close();
System.out.println("Data Inserted Successfully");
}
catch(Exception e)
{
System.out.println("Some problem..........");
}
}
}

/*
C:\java\jdbc>javac accjava.java
C:\java\jdbc>java accjava 1 Riya Mysore
Data Inserted Successfully */
Q24. Create a Java application program to display all the rows in the database.

import java.sql.*;
public class jdatasel
{
public static void main(String args[])
{
String str, str1,str2;
PreparedStatement ps;
ResultSet rs;
Connection con=null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connection = DriverManager.getConnection("jdbc:odbc:jdsnstud");
ps = connection.prepareStatement("select
nection.prepareStatement("select * from jstu
jstudent");
rs=ps.executeQuery();
while (rs.next())
{
str = rs.getString(1);
System.out.println(str);
str1 = rs.getString(2);
System.out.println(str1);
}
connection.close();
}
catch(Exception e)
{
System.out.println("Some problem......");
}
}
}

You might also like