Advance Java Programming Code
Topic 1- JDBC
Navigation path:
Step1: File new Java project create project name & finish
Step2: Right click on Project file New Class create class name &
finish.
Step3: Right click on Project file Build path Configure Build path
Library Add External Jars Download mysql-connector-j-8.3.0. (not
available means, just go to google & download MySQL connector j)
[Also must create database and table in MySQL Command line client]
1. Create Table program.
package com.s4;
import [Link];
import [Link];
import [Link];
import [Link];
public class CreateTable1
{
public static void main(String a[])
{
String str1="jdbc:mysql://localhost:3306/AN12345";
String str2="root";
String str3="root";
try
Prepared by Prof. Tamilarasi R
{
[Link]("[Link]");
Connectioncon=[Link](str1,str2,str3);
Statement st=[Link]();
[Link]("create table emp(empname varchar(20),empno
int,empaddr varchar(100))");
[Link]("****Connected****");
[Link]();
[Link]();
}
catch(SQLException e)
{
[Link]();
}
catch(ClassNotFoundException e)
{
[Link]();
}
}
}
2. Insert Table program.
package com.b1;
import [Link];
import [Link];
import [Link];
import [Link];
public class InsertT {
public static void main(String a[])
Prepared by Prof. Tamilarasi R
{
String str1="jdbc:mysql://localhost:3306/ AN12345";
String str2="root";
String str3="root";
Try
{
[Link]("[Link]");
Connection con=[Link](str1,str2,str3);
Statement st=[Link]();
[Link]("Insert into employee values('rajesh',146,'banglore')");
[Link]("Insert into employee values('chandra',149,'kadapa')");
[Link]("Insert into employee values('Nikhil',148,'vizag')");
[Link]("*******INSERTED******");
[Link]();
[Link]();
}
catch(SQLException e)
{
[Link]();
}
catch(ClassNotFoundException e)
{
[Link]();
Prepared by Prof. Tamilarasi R
}
}
}
3. Update Table program.
package com.b2;
import [Link];
import [Link];
import [Link];
import [Link];
public class Update {
public static void main(String a[])
{
String str1="jdbc:mysql://localhost:3306/ AN12345";
String str2="root";
String str3="root";
try{
[Link]("[Link]");
Connection con=[Link](str1,str2,str3);
Statement st=[Link]();
[Link]("Update employee set id=142 where id=149");
[Link]("*******UPDATED******");
[Link]();
Prepared by Prof. Tamilarasi R
[Link]();
}
catch(SQLException e)
{
[Link]();
}
catch(ClassNotFoundException e)
{
[Link]();
}
}
}
4. Delete Table program.
package com.b2;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class Display {
public static void main(String a[])
{
String str1="jdbc:mysql://localhost:3306/ AN12345";
String str2="root";
Prepared by Prof. Tamilarasi R
String str3="root";
Try
{
[Link]("[Link]");
Connection con=[Link](str1,str2,str3);
Statement st=[Link]();
ResultSet rs=[Link]("select * from employee");
while([Link]())
String name1=[Link]("empname");
String address1=[Link]("empaddress");
int id1=[Link]("id");
[Link]("empname is"+name1+",empid is
"+id1+",empaddress is"+address1);
}
[Link]("*******Display******");
[Link]();
[Link]();
}
catch(SQLException e)
{
[Link]();
}
catch(ClassNotFoundException e)
{
Prepared by Prof. Tamilarasi R
[Link]();
}
}
}
Topic 2- Swing and JDBC
Navigation path:
Step1: File new Java project create project name & finish
Step2: Right click on Project file New Other Select JFrame & Next
Give Package Name and Class Name & Finish.
Step3: Right click on Project file Build path Configure Build path
Library Add External Jars Download mysql-connector-j-8.3.0 and rt
jar file ( To get rt-jar, you have to go C-drive Program Files Java
jre1.8.0_341 lib and Select rt-jar )
[Note: Some program only need MySQL]
Prepared by Prof. Tamilarasi R
1. Eligible for Voting program
Design Part:
Source code:
Ok button:
final JLabel l3 = new JLabel(""); This code alone, I cut
[Link](75, 198, 277, 39); and pasted from down
to top. So, check
[Link](l3); carefully and do it.
JButton btnOk = new JButton("OK");
[Link](new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String name=[Link]();
int age=[Link]([Link]());
Prepared by Prof. Tamilarasi R
if(age>=18)
[Link](name+" is eligible for voting");
else
[Link](name+" is not eligible for voting");
}
});
Resent button:
JButton btnReset = new JButton("RESET");
[Link](new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
[Link]("");
textField_1.setText("");
[Link]("");
}
});
Prepared by Prof. Tamilarasi R
2. Username and Password program (you have create username
and password in Mysql)
Design part:
Source code:
Ok button:
final JLabel l3 = new JLabel(""); This code alone, I cut
and pasted from down
[Link](104, 220, 287, 30); to top. So, check
carefully and do it.
[Link](l3);
JButton btnNewButton = new JButton("OK");
[Link](new ActionListener()
10
Prepared by Prof. Tamilarasi R
{
public void actionPerformed(ActionEvent arg0)
{
String name=[Link]();
String pwd=[Link]();
String s1="jdbc:mysql://localhost:3306/test";
String s2="root";
String s3="root";
try
{
[Link]("[Link]");
Connection con=[Link](s1,s2,s3);
Statement st=[Link]();
ResultSet rs=[Link]("select * from users where
name='"+name+"' and pwd='"+pwd+"'");
if([Link]())
[Link]("***LOGIN SUCCESSFUL*******");
else
[Link]("***LOGIN NOT SUCCESSFUL*******");
}
catch(SQLException e)
{
[Link]();
11
Prepared by Prof. Tamilarasi R
}
catch(ClassNotFoundException e)
{
[Link]();
}
}
});
12
Prepared by Prof. Tamilarasi R
Topic 3- Servlet
Navigation path
Step1: File new other Dynamic Web Project Next &give project
name.
Step2: Right click on Dynamic web Project file New Class type class
name & finish.
Step3: Right click on Dynamic web Project file New HTML file
[Link] Finish.
How to install the server:
Step4: Window Show view Other Server.
Step5:
Have to click that line, which is shown in server,
Right click on Server link New Server Apache Tomcat v7.0
Server Finish click on Browser go to Download apache-
tomcat-7.0.47-windows-x64 folder select apache-tomcat-7.0.47
[You have to go google (type) download tomcat 7 click on Index of
/dist/tomcat/tomcat-7 click v7.0.47 (version) click bin/ file
13
Prepared by Prof. Tamilarasi R
Click apache-tomcat-7.0.47-windows-x64 folder (automatically
download)
Come back and select Src/ file [Link] . then
extract the file.
[Link] program using the servlet concept of username and query
executing?
[Link]:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>DemoServlet</title>
</head>
<body bgcolor=cyan>
<form method="get" action="ServletFirstDemo">
<pre>
Enter Your Name:<input type="text" name=t1>
<input type=submit><input type=reset>
</pre>
</form>
</body>
</html>
14
Prepared by Prof. Tamilarasi R
ServletDemo:
package com.s1;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/ServletFirstDemo")
public class ServletFirstDemo extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse
res) throws IOException,ServletException
{
PrintWriter pw=[Link]();
String s=[Link]("t1");
[Link]("<h1>Hello"+s);
[Link]();
}
15
Prepared by Prof. Tamilarasi R
[Link] program adding two numbers using servlets?
[Link]:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor=Yellow>
<form action="AddingServlet">
Enter 1st Number:<input type="text" name="t1"><br><br>
Enter 2nd Number:<input type="text" name="t2"><br><br>
<input type="Submit" value="Add">
</form>
</body>
</html>
Servlet code:
package com.s2;
import [Link];
import [Link];
import [Link];
import [Link];
16
Prepared by Prof. Tamilarasi R
import [Link];
import [Link];
import [Link];
@WebServlet("/AddingServlet")
public class AddingServlet extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse
res)throws IOException, ServletException
{
int a=[Link]([Link]("t1"));
int b=[Link]([Link]("t2"));
int c=a+b;
PrintWriter pw=[Link]();
[Link]("<html><body bgcolor=aaff99><h1>");
[Link]("ADDING is ...."+c);
[Link]("</h1></body></html>");
}
}
17
Prepared by Prof. Tamilarasi R
[Link] form using servlet:
Servlet code:
package com.s3;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse
res)throws IOException, ServletException
{
PrintWriter pw=[Link]();
String username=[Link]("t1");
String password=[Link]("t1");
if("telugu".equals(username)&& "123".equals(password))
18
Prepared by Prof. Tamilarasi R
{
[Link]("LogIn Sucess");
[Link]();
}
else
{
[Link]("LogIn Failed");
[Link]();
}
[Link]();
}
}
[Link]:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor=white>
<form action="LoginServlet"method="get">
<label for="username">user name:</label>
<input type="text" id="username" name="t1">
19
Prepared by Prof. Tamilarasi R
<label for="password">password:</label>
<input type="text" id="password" name="t2"><br><br>
<input type="Submit" value="Login">
</form>
</body>
</html>
[Link] program on servlet to cookie (servlet to servlet):
[Link]:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="Servlet1cookie" method="post">
Name:<input type="text" name="userName"/><br/>
<input type="submit" value="go">
</form>
</body>
</html>
20
Prepared by Prof. Tamilarasi R
Servlet1cookie:
package com.k1;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/Servlet1cookie")
public class Servlet1cookie extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse
response)throws IOException,ServletException
{
try
{
[Link]("text/html");
PrintWriter out=[Link]();
String n=[Link]("userName");
21
Prepared by Prof. Tamilarasi R
[Link]("Welcome "+n);
Cookie ck=new Cookie("uname",n);//creating cookie object
[Link](ck);
[Link]("<form action='Servlet2Cookie' method='post'>");
[Link]("<input type='submit' value='go'>"); [Link]("</form>");
[Link]();
}
catch(Exception e)
{
[Link](e);
}
}
}
Servlet2cookie:
package com.k1;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
22
Prepared by Prof. Tamilarasi R
import [Link];
public class Servlet2cookie extends HttpServlet
{
public void doPost(HttpServletRequest request,
HttpServletResponse response)throws IOException,ServletException
{
try
{
[Link]("text/html");
PrintWriter out=[Link]();
Cookie ck[]=[Link]();
[Link]("Hello "+ck[0].getValue());
[Link]();
}
catch(Exception e)
{
[Link](e);
}
}
23
Prepared by Prof. Tamilarasi R
Topic 4- JSP
Navigation Path:
Step1: File new other Dynamic Web Project Next &type project
name.
Step2: Right click on Dynamic web Project file New JSP file give
[Link] & finish.
Step3: Right click on Dynamic web Project file New HTML file
[Link] Finish.
[Need for servlet file means follow step 4] [Server required]
Step4: Right click on Dynamic web Project file New Class give class
name & finish.
1. JSP Code:
<%@ page language="java" contentType="text/html; charset=ISO-
8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "[Link]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-
8859-1">
<title>Insert title here</title>
24
Prepared by Prof. Tamilarasi R
</head>
<body><h1>Welcome JSP</h1>
</body>
</html>
2. JSP + Html + Servlet Code:
Create a Login page for Student Registration Form
Login [Link]:
package com.s2;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet("/LoginPage")
public class LoginPage extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
String uname=[Link]("uname");
25
Prepared by Prof. Tamilarasi R
String password=[Link]("password");
if([Link]("tamil")&& [Link]("123"))
{
[Link]("[Link]");
}
else
{
[Link]("[Link]");
}
}
}
[Link]:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Student Registration Form</title>
</head>
<body>
<form action="[Link]" method="post">
First name: <input type="text" name="firstName" />
<br/><br/> Last name: <input type="text" name="lastName" />
26
Prepared by Prof. Tamilarasi R
<br/><br/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
[Link]:
<%@ page language="java" contentType="text/html; charset=ISO-
8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "[Link]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-
8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="LoginPage"><pre>
Enter user name:<input type="text" name="uname"><br>
Enter password:<input type="password" name="password"><br>
<input type=submit value=login> <input type=reset></pre>
</form>
27
Prepared by Prof. Tamilarasi R
</body>
</html>
[Link]:
<%@ page language="java" contentType="text/html; charset=ISO-
8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "[Link]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-
8859-1">
<title> Student Confirmation Title</title></head>
<body>
<ul>
<li><p><b>First Name:</b>
<%= [Link]("firstName") %>
</p></li>
<li><p><b>Last Name:</b>
<%= [Link]("lastName") %>
</p></li>
</ul>
</body>
</html>
28
Prepared by Prof. Tamilarasi R
[Link]:
<%@ page language="java" contentType="text/html; charset=ISO-
8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "[Link]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-
8859-1">
<title>login success</title>
</head><h1> Login Successfully....</h1>
<body>
</body>
</html>
[Link]:
<%@ page language="java" contentType="text/html; charset=ISO-
8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "[Link]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-
8859-1">
29
Prepared by Prof. Tamilarasi R
<title>Unsuccessful login</title>
</head><h1> Unsuccessful login</h1>
<body>
</body>
</html>
3. JSP+JDBC+MySQL Code
[Link]:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="[Link]" method="post">
User name: <input type="text" name="usr"/>
password: <input type="password" name="pwd"/>
<input type="submit"/>
</form>
</body>
</html>
[Link]:
30
Prepared by Prof. Tamilarasi R
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="[Link]" method="post">
User name: <input type="text" name="usr"/>
password: <input type="password" name="pwd"/>
<input type="submit"/>
</form>
</body>
</html>
[Link]:
<%@ page language="java" contentType="text/html; charset=ISO-
8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "[Link]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-
8859-1">
31
Prepared by Prof. Tamilarasi R
<title>Insert title here</title>
</head>
<body>
<%@ page import="[Link].*"%>
<%
String user_id=[Link]("usr");
[Link]("user_id",user_id);
String pwd=[Link]("pwd");
[Link]("[Link]");
[Link]
con=[Link]("jdbc:mysql://localhost:3306/kou
stubh","root","root");
Statement st=[Link]();
ResultSet rs=[Link]("select * from login where
user_id='"+user_id+"'");
if([Link]())
{
if([Link](2).equals(pwd))
{
[Link]("welcome..... "+user_id);
}
else
{
[Link]("Invalid password try again");
32
Prepared by Prof. Tamilarasi R
}
}
else
%>
</body>
</html>
[Link]:
<%@ page language="java" contentType="text/html; charset=ISO-
8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "[Link]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-
8859-1">
<title>Insert title here</title>
</head>
<body>
<%@ page import="[Link].*" %>
<%
String usr=[Link]("usr");
[Link]("user_id",usr);
String pwd=[Link]("pwd");
33
Prepared by Prof. Tamilarasi R
[Link]("[Link]");
[Link]
con=[Link]("jdbc:mysql://localhost:3306/kou
stubh","root","root");
Statement st=[Link]();
ResultSet rs;
int i=[Link]("insert into login
values('"+usr+"','"+pwd+"')");
[Link]("Registered...");
%>
</body>
</html>
Topic 5- Hibernate
Navigation path:
Step1: File new other Maven Project next [Link]
[Link] quickstart 1.1 or 1.4 ( quickstart not is mavan project
means [go to google (type) mavan archetype quickstart 1.1 copy
group id, artifact id, version, URL ]
Step2: Right click on maven project folder New Class [including
package name]
34
Prepared by Prof. Tamilarasi R
For hibernate file we have to install JBoss tool
Step3: Help Eclipse marketplace (type) hibernate and search
JBoss tool (with version) click install
Step4: Right click on project folder new other (type)
hibernate select Hibernate. Configure .[Link] finish one
window will open, in that you have fill MySQL URL, local host address,
database, username & password click ok.
How to get maven dependency
Hibernate dependency:
Step5: Open Google search (type) Maven dependency click Maven
Repository: Search/Browse/Explore (In search, have to type) hibernate
Hibernate Core Relocation select version 8.0.33 copy dependency
paste to [Link]
MySQL connector dependency:
(In search, have to type) Mysql connector j MySQL Connector Java
select version 6.3.1 copy dependency paste to [Link]
(In MySQL have to create database alone and check whether
your table is created automatically or not)
1. To create tables, update, modify, and delete using Hibernate
concept.
[Link]:
package [Link];
import [Link];
35
Prepared by Prof. Tamilarasi R
import [Link];
import [Link];
import [Link];
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
Configuration cf=new Configuration();
[Link]().addAnnotatedClass([Link]);
SessionFactory sf=[Link]();
Session ss=[Link]();
Transaction tr=[Link]();
[Link]();
RegisterStu s1=new RegisterStu();
[Link](888);
[Link]("sathish");
[Link](s1);
36
Prepared by Prof. Tamilarasi R
/*RegisterStu s2=[Link]([Link],888);
[Link]("sumit");
[Link](s2);*/
/*RegisterStu s3=[Link]([Link],333);
[Link]("the deleted record" + [Link]());
[Link](s3);*/
[Link]();
}
}
[Link]:
package [Link];
import [Link];
import [Link];
@Entity
public class RegisterStu {
@Id
private int Rno;
private String Name;
37
Prepared by Prof. Tamilarasi R
public int getRno() {
return Rno;
}
public void setRno(int rno) {
Rno = rno;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
}
[Link]:
<property name="[Link].driver_class">[Link]</property>
<property name="[Link]">root</property>
<property name="[Link]">jdbc:mysql://localhost:3306/tami</property>
<property name="[Link]">root</property>
<property name="[Link]">[Link]</property>
<property name="[Link]">create</property>
<property name="hibernate.show_sql">true</property>
</session-factory>
</hibernate-configuration>
38
Prepared by Prof. Tamilarasi R
DemoMaven/[Link]
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- [Link]
java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<!-- [Link]
core -->
39
Prepared by Prof. Tamilarasi R
40
Prepared by Prof. Tamilarasi R