First program in java
Last Updated on : 19th Oct 2024 15:12:52 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("</tr>");
}
out.println("</table>");
out.println("</center>");
out.println("</body>");
out.println("</html>");
}
Become a first user to comment