package com;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import com.Student;
public class Datebase {
public static Connection getConnection() {
Connection con = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=stu", "sa","123");
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
public String getCharset(String str) {
String newStr = "";
try {
newStr = new String(str.getBytes("ISO-8859-1"), "GB2312");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return newStr;
}
public int insert( Student sdb) {
//ArrayList list=new ArrayList();
int b=0;
Connection con = getConnection();
try{
String Id=sdb.getId();
String name=sdb.getName();
String password=sdb.getPassword();
String sex=sdb.getSex();
String birthday=sdb.getBirthday();
String working=sdb.getWorking();
String phone=sdb.getPhone();
String post=sdb.getPost();
Statement st=con.createStatement();
st.executeUpdate("insert into stuinfo values('"+Id+"','"+name+"','"+password+"','"+sex+"','"+birthday+"','"+working+"','"+phone+"','"+post+"')");
// list=select1();
st.close();
con.close();
}catch(SQLException e){
e.printStackTrace();
}
return b;
}
public ArrayList<Student> select1(){
Connection con=null;
ResultSet rs=null;
Statement st =null;
ArrayList<Student> list = new ArrayList<Student>();
String Id,name,password,sex,birthday,working,phone,post;
con = Datebase.getConnection();
try {
st = con.createStatement();
rs = st.executeQuery("select * from stuinfo ");
while (rs.next()) {
Id = rs.getString("Id") + "";
name = (rs.getString("name"));
password = rs.getString("password") + "";
sex = rs.getString("sex") + "";
birthday = (rs.getString("birthday"));
working = (rs.getString("working"));
phone = (rs.getString("phone"));
post = (rs.getString("post"));
Student std = new Student();
std.setId(Id);
std.setName(name);
std.setPassword(password);
std.setSex(sex);
std.setBirthday(birthday);
std.setWorking(working);
std.setPhone(phone);
std.setPost(post);
list.add(std);
}
//st.close();
//rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
public Student select(String cr) {
//ArrayList list = new ArrayList();
String Id,name,password,sex,birthday,working,phone,post;
Student std = new Student();
Connection con = Datebase.getConnection();
ResultSet rs=null;
try {
Statement st = con.createStatement();
rs = st.executeQuery("select * from stuinfo where name='"+cr+"'");
if (rs.next()) {
Id = rs.getString("Id") + "";
name = (rs.getString("name"));
password = (rs.getString("password"));
sex = rs.getString("sex") + "";
birthday = (rs.getString("birthday"));
working = (rs.getString("working"));
phone = (rs.getString("phone"));
post = (rs.getString("post"));
//Student std = new Student();
std.setId(Id);
std.setName(name);
std.setPassword(password);
std.setSex(sex);
std.setBirthday(birthday);
std.setWorking(working);
std.setPhone(phone);
std.setPost(post);
//list.add(std);
}
st.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
return std;
}
public ArrayList<Student> delete(String name1){
ArrayList<Student> list=new ArrayList<Student>();
Connection con=Datebase.getConnection();
try{
Statement st=con.createStatement();
st.executeUpdate("delete from stuinfo where name='"+name1+"'");
//=select1();
st.close();
con.close();
}catch(SQLException e){
e.printStackTrace();
}
return list;
}
private static Connection con;
public static void update(Student sdb,String name1){
con=getConnection();
ResultSet rs=null;
try{
Statement st=con.createStatement();
st.executeUpdate("update stuinfo set Id='"+sdb.getId()+"',name='"+sdb.getName()+"',password='"+sdb.getPassword()+"',sex='"+sdb.getSex()+"',birthday='"+sdb.getBirthday()+"',working='"+sdb.getWorking()+"' ,phone='"+sdb.getPhone()+"',post='"+sdb.getPost()+"' where name='"+name1+"'");
// sdb.select();
st.close();
con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Jsp+JavaBean+Servlet(绝对正确).rar (38个子文件)
Jsp+JavaBean+Servlet(绝对正确)
stu_log.ldf 1024KB
ding
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.wst.common.component 450B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 500B
org.eclipse.jdt.core.prefs 395B
src
Serv
xianshiServlet.java 1KB
com
Datebase.java 5KB
Student.java 1KB
fourServ
update1.java 2KB
insert.java 2KB
update.java 2KB
select.java 2KB
delete.java 1KB
.project 2KB
WebRoot
a2.jsp 1KB
select2.jsp 1KB
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 2KB
classes
Serv
xianshiServlet.class 2KB
com
Datebase.class 5KB
Student.class 2KB
fourServ
insert.class 3KB
delete.class 2KB
update.class 2KB
update1.class 3KB
select.class 2KB
lib
sqljdbc.jar 570KB
show2.jsp 2KB
index.jsp 1KB
show.jsp 2KB
delete2.jsp 1KB
insert2.jsp 2KB
update2.jsp 665B
.mymetadata 285B
.classpath 447B
.myeclipse
stu.mdf 3MB
共 38 条
- 1
资源评论
- 忆殇垣野2014-06-07不是绝对正确的。。。。我在index页面进去以后点击“是”以后页面就开始出错了
ann855
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功