package com.controller;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import com.methd.usdata;
import com.util.conn;
public class controller
{
public int insdata(usdata u)
{
conn cn = new conn();
Connection cnn = cn.Mkconn();
int i = 0;
try
{
Statement st = cnn.createStatement();
i = st.executeUpdate("insert into stud(name,password,email,mobile,country,state,city,gender,hobby,file) values('"+u.getUname()+"','"+u.getPassword()+"','"+u.getEmail()+"','"+u.getMobile()+"','"+u.getCountry()+"','"+u.getState()+"','"+u.getCity()+"','"+u.getGender()+"','"+u.getHobby()+"','"+u.getImage()+"')");
return i;
}
catch(Exception e)
{
System.out.println("insert error "+e.getMessage());
}
finally
{
try {
cnn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return i;
}
public ArrayList<usdata> getalldata()
{
conn cn = new conn();
Connection cnn = cn.Mkconn();
usdata ud;
try
{
Statement st = cnn.createStatement();
ResultSet rs = st.executeQuery("select * from stud");
while(rs.next())
{
ArrayList<usdata> al = new ArrayList<>();
ud = new usdata();
ud.setId(rs.getString("id"));
ud.setUname(rs.getString("name"));
ud.setPassword(rs.getString("password"));
ud.setEmail(rs.getString("email"));
ud.setMobile(rs.getString("mobile"));
ud.setCountry(rs.getString("country"));
ud.setState(rs.getString("state"));
ud.setCity(rs.getString("city"));
ud.setGender(rs.getString("gender"));
ud.setHobby(rs.getString("hobby"));
ud.setImage(rs.getString("file"));
al.add(ud);
return al;
}
}
catch (Exception e)
{
System.out.println("Data fetch error "+ e);
}
finally
{
try {
cnn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
public ArrayList<usdata> getdatabyid(usdata u)
{
ArrayList<usdata> al = new ArrayList<usdata>();
conn cn = new conn();
Connection cnn = cn.Mkconn();
usdata ud;
try
{
Statement st = cnn.createStatement();
ResultSet rs = st.executeQuery("select * from stud where id = '"+u.getId()+"'");
while(rs.next())
{
ud = new usdata();
ud.setId(rs.getString("id"));
ud.setUname(rs.getString("name"));
ud.setPassword(rs.getString("password"));
ud.setEmail(rs.getString("email"));
ud.setMobile(rs.getString("mobile"));
ud.setCountry(rs.getString("country"));
ud.setState(rs.getString("state"));
ud.setCity(rs.getString("city"));
ud.setGender(rs.getString("gender"));
ud.setHobby(rs.getString("hobby"));
ud.setImage(rs.getString("file"));
al.add(ud);
return al;
}
}
catch (Exception e)
{
System.out.println("Data fetch error "+ e);
}
finally
{
try {
cnn.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
return al;
}
public int update_data(usdata u)
{
int i = 0;
try
{
conn cn = new conn();
Connection cnn = cn.Mkconn();
Statement st = cnn.createStatement();
i = st.executeUpdate("update stud set name= '"+u.getUname()+"',password = '"+u.getPassword()+"',email = '"+u.getEmail()+"',mobile = '"+u.getMobile()+"',country = '"+u.getCountry()+"',state = '"+u.getState()+"',city = '"+u.getCity()+"',gender = '"+u.getGender()+"',hobby = '"+u.getHobby()+"',file = '"+u.getImage()+"' where id = '"+u.getId()+"'");
return i;
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
return i;
}
public int deldata(usdata u)
{
int i = 0;
try
{
conn cn = new conn();
Connection cnn = cn.Mkconn();
Statement st = cnn.createStatement();
i = st.executeUpdate("delete from stud where id = '"+u.getId()+"'");
return i;
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
return i;
}
}
评论0