谁能给一个关于jsp登录连接数据库然后进行增删改查的代码,我们老师要,急
2个回答
展开全部
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;
}//改
}
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;
}//改
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询