First program in java
Last Updated on: 6th Feb 2025 12:25:18 PM
class FirstProgram{
public static void main(String args[]){
System.out.println("Hello Java");
}
}
This is our first program of java which will print "Hello Java" in console screen.
Save the above file as FirstProgram.java.
How To Compile :
Javac FirstProgram.java
How to Run:
java FirstProgram
========================================================================
Aggregation in java
Address.java
public class Address {
String city,state,country;
Address(String city, String state,String country)
{
this.city=city;
this.state=state;
this.country=country;
}
}
Student,java
/**
* Student
*/
public class Student {
int roll;
String name;
Address address;
Student(int roll, String name, Address address) {
this.roll = roll;
this.name = name;
this.address = address;
}
void dispaly() {
System.out.println("My name is " + this.name + " Roll is " + this.roll);
System.out.println("City :" + address.city + " State : " + address.state +
" Country : " + address.country);
}
public static void main(String[] args) {
Address a = new Address("Mumbai", "Maharastra", "india");
Address a1 = new Address("Pune", "Maharastra", "india");
Student s = new Student(101, "sandip", a);
Student s1 = new Student(102, "Amit", a1);
s.dispaly();
s1.dispaly();
}
}
JDBC
import java.sql.*; // set path// : set path=C:Program FilesJavajdk-17.0.2in
class Database{ // run javac -cp mysql-connector-java-8.0.29.jar;. Database.java
public static void main(String args[]){
try{
Class.forName("com.mysql.cj.jdbc.Driver");
//Connection con=DriverManager.getConnection("jdbc:mysql://localhost:4306/mydata","root","sandip@123");//xamp port
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sandip","root","sandip@1234"); //mysql port
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from register");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();
}catch(Exception e){ System.out.println(e);}
}
}
insertdata
try ( PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
String name = request.getParameter("name");
String password = request.getParameter("password");
String email = request.getParameter("email");
String number = request.getParameter("number");
String address = request.getParameter("address");
Class.forName("com.mysql.jdbc.Driver");
// get the connection
Connection co = DriverManager.getConnection("jdbc:mysql://localhost:3306/bhaskar","root","sandip@123");
// get the PS
//PreparedStatement ps = co.prepareStatement("insert into employee values(?,?,?,?,?)");
PreparedStatement ps = co.prepareStatement("INSERT INTO employee (name,password,email,number,address) values (?, ? , ?, ?, ?)");
// provide values in place of ? mark
ps.setString(1, name);
ps.setString(2, password);
ps.setString(3, email);
ps.setString(4, number);
ps.setString(5, address);
// execute the sql statement
ps.executeUpdate();
// get print writer to show the data on browser
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet MyServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<center>");
out.println("<font style="font-size: 30px" color="green">Account is created</font>");
// this is used to include the html page in servlet
request.getRequestDispatcher("index.html").include(request, response);
out.println("<a href="Login.html">click here for login</a>");
out.println("</center>");
//out.println("<h1>My Name : " + name+"<br> Password : "+password+"<br> Email : "+email+"<br> Number : "+number+ "<br>Address : " +address+"</h1>");
out.println("</body>");
out.println("</html>");
// load driver class
// show a link on web browser at the client size
}catch (Exception e)
{
// cause and line no of exception will be printed on server console
e.printStackTrace();
}
fetch data from database
Statement st;
ResultSet rs;
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:4306/mydata","root","sandip@123");
st=con.createStatement();
String query = "select * from student";
rs=st.executeQuery(query);
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet ViewRecord</title>");
out.println("</head>");
out.println("<body>");
out.println("<center>");
out.println("<table cellspacing="0" width ="500px" border="1">"); // use single inverted
out.println("<tr>");
out.println("<td> Name :</td>");
out.println("<td> Address :</td>");
out.println("<td> Update:</td>");
out.println("<td> Delete:</td>");
out.println("</tr>");
while(rs.next())
{
String name=rs.getString("name");
String address=rs.getString("address");
out.println("<tr>");
out.println("<td>"+name+"</td>");
out.println("<td>"+address+"</td>");
out.println("<td> <a href="Edit?name="+rs.getString("name")+"">Edit</a> </td>");// use single inverted comma
out.println("<td> <a href="Delete?name="+rs.getString("name")+"">Delete</a> </td>"); //use single inverted
out.println("<td> <a href='Edit?id="+rs.getString("id")+"'>Edit</a> </td>");
out.println("<td> <a href='Delete?id="+rs.getString("id")+"'>Delete</a> </td>");
out.println("</tr>");
}
out.println("</table>");
out.println("</center>");
out.println("</body>");
out.println("</html>");
}
Delete
//String emp_id=request.getParameter("id");
Connection cn1;
Class.forName("com.mysql.cj.jdbc.Driver");
cn1=DriverManager.getConnection("jdbc:mysql://localhost:3306/bhaskar","root","sandip@123");
PreparedStatement statement = cn1.prepareStatement("delete from employee WHERE id = ? "); //ps.setString(1, (String) jTable1. getValueAt(x, 0));
statement.setString(1,request.getParameter("id"));
int row = statement.executeUpdate();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet UpdateServlet</title>");
out.println("</head>");
out.println("<body>");
if (row > 0)
{
out.println("<h1><font color='green'>Sucessfully deleted</font></h1>");
}
out.println("</body>");
out.println("</html>");
}
catch (Exception e)
{
// cause and line no of exception will be printed on server console
e.printStackTrace();
}
Edit
/* TODO output your page here. You may use following sample code. */
String eid =request.getParameter("id");
Statement st1;
Connection cn;
ResultSet rs;
Class.forName("com.mysql.cj.jdbc.Driver");
cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/bhaskar","root","sandip@123");
st1=cn.createStatement();
String p="select * from employee where id ="+eid+" ";
rs=st1.executeQuery(p);
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Edit</title>");
out.println("</head>");
out.println("<body>");
while (rs.next())
{
out.println("<form method=\"POST\" action=\"UpdateServlet\">");
out.println("<center><table width='500px'>");
out.println("<tr> <td><input type ='hidden' name='id' value='"+rs.getString("id")+"'></td>");
out.println("<tr><td> Name </td> <td><input type ='text' name= 'name' value='"+rs.getString("name")+"'></td></tr>");
out.println("<tr><td> password : </td> <td><input type ='text' name='password'value='"+rs.getString("password")+"'></td></tr");
out.println("<tr><td> Email : </td> <td><input type ='text' name ='email' value='"+rs.getString("email")+"'></td></tr");
out.println("<tr><td> Mobile No : </td> <td><input type ='text' name ='number' value='"+rs.getString("number")+"'></td></tr");
out.println("<tr><td> Address </td> <td><input type ='text' name='address' value='"+rs.getString("address")+"'></td></tr");
out.println("<tr><td></td><td><br><br><input type ='submit' value='Update'></td></tr>");
out.println("</table></center>");
out.println("</form>");
out.println("</tr>");
}
out.println("</body>");
out.println("</html>");
st1.close();
cn.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
Become a first user to comment