谁能给一个关于jsp登录连接数据库然后进行增删改查的代码,我们老师要,急

 我来答
zhengdayc
2015-08-09 · TA获得超过431个赞
知道小有建树答主
回答量:531
采纳率:0%
帮助的人:228万
展开全部
package Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.mysql.jdbc.Statement;
import com.sun.org.apache.xpath.internal.operations.And;

import Tool.JDBCUtils;

import DataBean.Article;
import DataBean.articleType;

public class articleDao {
Connection connection =null;
PreparedStatement preparedStatement=null;
Article article=null;
public articleDao(){
connection=JDBCUtils.getConnection();
}
public int insertArticle(Article article){
try {

String sql="insert into article (title,content,publishTime,typeId,userId) values(?,?,?,?,?)";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1, article.getTitle());
preparedStatement.setString(2, article.getContent());
preparedStatement.setString(3, article.getPublishTime());
preparedStatement.setInt(4, article.getTypeId());
preparedStatement.setInt(5, article.getUserId());
return preparedStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}finally{
JDBCUtils.release(connection, preparedStatement, null);
}
}//增
public List selectArticle() {
List list = new ArrayList();
Article form = null;
Connection con = null ;
PreparedStatement ps = null ;
ResultSet rs = null;
try {
con = JDBCUtils.getConnection();
String sql = "select articleId,title,content,publishTime,typeId from article";
ps=con.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
form = new Article();
form.setArticleId(rs.getInt(1));
form.setTitle(rs.getString(2));
form.setContent(rs.getString(3));
form.setPublishTime(rs.getString(4));
form.setTypeId(rs.getInt(5));
list.add(form);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JDBCUtils.release(con, ps, rs);
}
return list;
}//查
public int deleteArticle(int articleId){
Connection conn=null;
PreparedStatement pstmt=null;
try {
String sql="delete from article where articleId='" + articleId+ "'";
conn=JDBCUtils.getConnection();
pstmt=conn.prepareStatement(sql);
int rtn=pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JDBCUtils.release(conn, pstmt, null);
}
return 0;
}//删
public int updateArticle(Article article){
Connection conn=null;
PreparedStatement pstmt=null;
int articleId=article.getArticleId();
try {
String sql="update article set title=?,content=?,publishTime=?,typeId=? where articleId='" + articleId+ "'";
conn=JDBCUtils.getConnection();
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, article.getTitle());
pstmt.setString(2, article.getContent());
pstmt.setString(3, article.getPublishTime());
pstmt.setInt(4, article.getTypeId());
int rtn=pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JDBCUtils.release(conn, pstmt, null);
}
return 0;
}//改

}
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
BORN815
2015-08-09 · 超过10用户采纳过TA的回答
知道答主
回答量:35
采纳率:0%
帮助的人:18万
展开全部
明天下午我发给你,邮箱说下。
追问
55 24657 35  @ q   q  .com
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式