package shiyan1;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import shiyan1.Bean;
import shiyan1.Hahahha;
public class Dao {
public boolean add(Bean bean) {
//get date from us.
String sql ="insert into student (id,name,aclass,kecheng,score)"
+ "values('" + bean.getId() + "','" + bean.getName()+ "','" + bean.getAclass() + "','" + bean.getKecheng()+ "','" + bean.getScore()+ "')";
Connection conn = Hahahha.getConn();
Statement state =null;
boolean f = false;
int a =0;
try {
state =conn.createStatement();
a=state.executeUpdate(sql);
}
catch( Exception e){
e.printStackTrace();
}
finally {
Hahahha.close(state, conn);
}
if(a>0) {
f=true;
}
return f;
}
//delete the date from us.
public boolean delete (String id) {
boolean f = false;
String sql = "delete from student where id='" + id + "'";
Connection conn = Hahahha.getConn();
Statement state = null;
int a = 0;
try {
state = conn.createStatement();
a = state.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
finally {
Hahahha.close(state, conn);
}
if (a > 0) {
f = true;
}
return f;
}
//change
public String find(String id)
{
String sql = "select * from student ";
if (id!= "") {
sql += "id like '" + id +"'";
}
Connection conn = Hahahha.getConn();
Statement state = null;
ResultSet rs = null;
String userid2="@";
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
userid2 = rs.getString("id");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
Hahahha.close(rs, state, conn);
}
return userid2;
}
public Bean getUserbyid(String id1) {
String sql = "select * from student name ='" + id1 + "'";
Connection conn = Hahahha.getConn();
Statement state = null;
ResultSet rs = null;
Bean user3 = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
String id=rs.getString("id");
String name=rs.getString("name");
String aclass=rs.getString("aclass");
String kecheng=rs.getString("kecheng");
String score=rs.getString("score");
user3= new Bean(id,name,aclass,kecheng,score);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Hahahha.close(rs, state, conn);
}
return user3;
}
public boolean update(Bean user,String id)
{
String sql="update student set name='" + user.getName() + "',aclass='" + user.getAclass() + "',kecheng='" + user.getKecheng() + "',score='" + user.getScore() + "' id='" + id + "'";
Connection conn = Hahahha.getConn();
Statement state = null;
boolean f = false;
int a = 0;
try {
state = conn.createStatement();
a = state.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
Hahahha.close(state, conn);
}
if (a > 0) {
f = true;
}
return f;
}
}