package cn.edu.hbcf.connection;
import java.sql.*;
import java.util.List;
import java.util.ArrayList;
import cn.edu.hbcf.dao.Category;
import cn.edu.hbcf.dao.News;
import cn.edu.hbcf.dao.Product;
import cn.edu.hbcf.dao.User;
public class DBConn {
String DRIVER="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String URL="jdbc:microsoft:sqlserver://localhost:1433;databasename=db_net";
String USERNAME="sa";
String PWD="123";
Connection con=null;
Statement stat=null;
PreparedStatement pstat=null;
ResultSet rs=null;
public Object getConnetion;
public Connection getConnection(){
try{
Class.forName(DRIVER);
con=DriverManager.getConnection(URL,USERNAME,PWD);
}catch(ClassNotFoundException e){
System.out.println("未成功注册"+e.toString());
}catch(SQLException e){
System.out.println("数据库连接失败"+e.toString());
}
return con;
}
/**
* 查询用户
* @param user
* @return true/false
*/
public boolean check(User user){
boolean flag=false;
try{
this.getConnection();
String sql="select * from tb_usertable where name=? and password=?";
pstat=con.prepareStatement(sql);
pstat.setString(1, user.getName());
pstat.setString(2, user.getPassword());
rs=pstat.executeQuery();
if(rs.next()){
flag=true;
}
this.closeAll();
}catch(SQLException e){
System.out.println("用户查询失败"+e.toString());
}
return flag;
}
/**
*查找全部新闻
* @return news
*/
public List<News> queryAllNews(){
List<News> list=new ArrayList<News>();
try{
this.getConnection();
String sql="select * from tb_news";
pstat=con.prepareStatement(sql);
rs=pstat.executeQuery();
while(rs.next()){
News news=new News();
news.setId(rs.getInt("id"));
news.setAuthor(rs.getString("author"));
news.setContent(rs.getString("content"));
news.setTitle(rs.getString("title"));
news.setSubmittime(rs.getString("submittime"));
list.add(news);
}
this.closeAll();
}catch(SQLException e){
}
return list;
}
/**
*
* @param news
* @return flag
* 如果成功更新一条新闻,则返回1,否则返回0;
*/
public int newsUpdate(News news){
int flag=0;
try{
this.getConnection();
String sql="update tb_news set title=?,content=?,author=?,submittime=? where id=?";
pstat=con.prepareStatement(sql);
pstat.setString(1, news.getTitle());
pstat.setString(2, news.getContent());
pstat.setString(3, news.getAuthor());
pstat.setString(4, news.getSubmittime());
pstat.setInt(5,news.getId());
flag=pstat.executeUpdate();
this.closeAll();
}catch(SQLException e){
System.out.println("更新新闻产生异常"+e.toString());
}
return flag;
}
/**
*
* @param id
* @return flag
* 如果删除新闻成功,返回1,否则返回0;
*/
public int newsDel(String id){
int flag=0;
News news=new News();
news.setId(Integer.parseInt(id));
try{
this.getConnection();
String sql="delete from tb_news where id=?";
pstat=con.prepareStatement(sql);
pstat.setInt(1, news.getId());
flag=pstat.executeUpdate();
this.closeAll();
}catch(SQLException e){
System.out.println("删除新闻产生异常"+e.toString());
}
//System.out.println(flag);
return flag;
}
/**
* 依据id查询新闻
* @param id
* @return news
*/
public News queryNewsById(String id){
News news=null;
try{
this.getConnection();
String sql="select * from tb_news where id=?";
pstat=con.prepareStatement(sql);
pstat.setInt(1,Integer.parseInt(id));
rs=pstat.executeQuery();
if(rs.next()){
news=new News();
news.setId(rs.getInt("id"));
news.setAuthor(rs.getString("author"));
news.setContent(rs.getString("content"));
news.setTitle(rs.getString("title"));
news.setSubmittime(rs.getString("submittime"));
}
this.closeAll();
}catch(SQLException e){
System.out.println("查询一条新闻出错"+e.toString());
}
return news;
}
/**
* 添加一条新闻
* @param news
* @param
* @return flag
* 如果成功插入一条新闻,则返回1,否则返回0
*/
public int addNews(News news){
int flag=0;
try{
this.getConnection();
String sql="insert into tb_news values(?,?,?,?)";
pstat=con.prepareStatement(sql);
pstat.setString(1, news.getTitle());
pstat.setString(2, news.getContent());
pstat.setString(3, news.getAuthor());
pstat.setString(4, news.getSubmittime());
flag=pstat.executeUpdate();
this.closeAll();
}catch(SQLException e){
System.out.println("添加一条新闻异常"+e.toString());
}
return flag;
}
/**
* 查询所有类别
* @return category
*/
public List<Category> queryAllCategory(){
List<Category> list=new ArrayList<Category>();
try{
this.getConnection();
String sql="select * from tb_category";
pstat=con.prepareStatement(sql);
rs=pstat.executeQuery();
while(rs.next()){
Category category=new Category();
category.setId(rs.getInt("id"));
category.setCategoryname(rs.getString("categoryname"));
category.setSubmittime(rs.getString("submittime"));
list.add(category);
}
this.closeAll();
}catch(SQLException e){
System.out.println("查询类别失败"+e.toString());
}
return list;
}
/**
* 依据id查询类别
* @param id
* @return category
*/
public Category queryCategoryById(String id){
Category category=null;
try{
this.getConnection();
String sql="select * from tb_category where id=?";
pstat=con.prepareStatement(sql);
pstat.setInt(1,Integer.parseInt(id));
rs=pstat.executeQuery();
if(rs.next()){
category=new Category();
category.setId(rs.getInt(1));
category.setCategoryname(rs.getString("categoryname"));
category.setSubmittime(rs.getString("submittime"));
}
this.closeAll();
}catch(SQLException e){
System.out.println("查询类别失败"+e.toString());
}
return category;
}
/**
* 添加类别
* return flag
*/
public int categoryAdd(Category category){
int flag=0;
try{ this.getConnection();
String sql="insert into tb_category values(?,?)";
pstat=con.prepareStatement(sql);
pstat.setString(1, category.getCategoryname());
pstat.setString(2, category.getSubmittime());
flag=pstat.executeUpdate();
this.closeAll();
}catch(SQLException e){
System.out.print("添加类别失败"+e.toString());
}
return flag;
}
/**
* 修改类别
* @param category
* @return 0/1
*/
public int updateCategory(Category category){
int flag=0;
try{ this.getConnection();
String sql="update tb_category set categoryname=?,submittime=? where id=?";
pstat=con.prepareStatement(sql);
pstat.setString(1, category.getCategoryname());
pstat.setStri
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
企业门户网站(利用jsp技术并连接数据库) (145个子文件)
DBConn.class 11KB
CountPage.class 2KB
Product.class 1KB
News.class 1KB
CodeFilter.class 1KB
Category.class 952B
User.class 910B
MyEncoder.class 794B
.classpath 826B
org.eclipse.wst.jsdt.ui.superType.container 49B
style.css 971B
5.gif 57KB
4.gif 56KB
ht03.gif 52KB
2.gif 49KB
5.gif 49KB
4.gif 49KB
7.gif 46KB
3.gif 30KB
4.gif 27KB
1.gif 27KB
3.gif 25KB
4.gif 24KB
ht05.gif 23KB
ht05new2.gif 23KB
32.gif 23KB
31.gif 22KB
33.gif 22KB
30.gif 21KB
ht06.gif 21KB
2.gif 13KB
ht02.gif 13KB
10.gif 11KB
5.gif 11KB
8.gif 10KB
ht01.gif 10KB
5.gif 10KB
8.gif 10KB
7.gif 10KB
14.gif 10KB
9.gif 10KB
4.gif 10KB
6.gif 10KB
6.gif 10KB
6.gif 8KB
3.gif 7KB
8.gif 7KB
7.gif 6KB
1.gif 6KB
11.gif 6KB
ht04.gif 6KB
ht043.gif 6KB
ht045.gif 6KB
ht042.gif 6KB
ht044.gif 6KB
ht041.gif 5KB
5.gif 2KB
2.gif 2KB
1.gif 2KB
12.gif 2KB
3.gif 1KB
1.gif 1KB
16.gif 946B
15.gif 857B
7.gif 645B
8.gif 635B
2.gif 519B
2.gif 517B
3.gif 517B
8.gif 517B
13.gif 484B
9.gif 458B
11.gif 456B
1.gif 456B
6.gif 456B
9.gif 403B
10.gif 264B
17.gif 231B
6.gif 225B
12.gif 223B
7.gif 157B
分隔符.gif 43B
分隔符.gif 43B
分隔符.gif 43B
分隔符.gif 43B
gswh.html 3KB
jszc.html 3KB
msbase.jar 280KB
msbase.jar 280KB
commons-beanutils-1.7.0.jar 184KB
commons-io-1.3.2.jar 86KB
mssqlserver.jar 66KB
mssqlserver.jar 66KB
msutil.jar 58KB
msutil.jar 58KB
commons-fileupload-1.2.1.jar 56KB
DBConn.java 13KB
News.java 1KB
CountPage.java 1KB
Product.java 992B
共 145 条
- 1
- 2
资源评论
- 文小王这Y2014-01-09不错,可惜有很多要改
- sspg622014-04-04还行,就是不怎么熟悉,搭起来费了不少时间
- NNnDota2013-01-25急需啊,只是数据库是用的SQL server,要改成MySql的。
- clbblood2013-09-04SQL server
fhthaikuotiankong
- 粉丝: 1
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 记录了贪心,动态规划等算法基本思想与设计.zip
- 基于菲阿里基本模型,以及MACD RSI BooL 等技术指标 构建一套基于贪心算法策略的智能投顾模型.zip
- oj算法代码-贪心算法.zip
- 基于yolov8行人检测源码+模型.zip
- 公开整理-地级市-绿色专利申请、授权数据集(2000-2022年).xlsx
- 基于Transformer模型的图像质量评分模型实现源码+详细说明文档.zip
- CST电磁场仿真+线性螺旋电感+建模步骤细节和RLC端口配置+CST高级建模操作
- 大数据库实验的报告材料材料(word文档良心出品).doc
- AIS2024 valid
- 最入门的爬虫代码 python.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功