jsp 数据链接+新闻增删改查

package com.dao;
import java.sql.*;
import java.util.*;
import com.model.News;

public class Database
{
  static Connection con;
  static Statement sql;
  static ResultSet res;

  private static Connection conn()
  {

    try {
        Class.forName("com.mysql.jdbc.Driver");

        System.out.println("驱动成功......");
      }
    catch (ClassNotFoundException e)
    {
      e.printStackTrace();
    }

    try
    {
      con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydata", "root", "123456");
      System.out.println("数据库连接成功......");
    }
    catch (SQLException e)
    {
      e.printStackTrace();
    }
    return con;

  }

  

  //新闻列表查询
  public List<Object> datashow()
  {
    List<Object> list=new ArrayList<Object>();

    try
    {
      sql=conn().createStatement();
      res=sql.executeQuery("SELECT * FROM j_new");
      while(res.next())
      {
        News model=new News();
        model.setId(Integer.parseInt(res.getString("id")));
        model.setTitle(res.getString("title"));
        model.setContent(res.getString("content"));
        list.add(model);
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return list;

  }

  //新闻详细页面
  public News newDetail(int id)
  {
    News model=new News();

    try
    {
      sql=conn().createStatement();
      res=sql.executeQuery("select * from j_new where id="+id);
      if(res.next())
      {
        model.setId(Integer.parseInt(res.getString("id")));
        model.setTitle(res.getString("title"));
        model.setType(res.getString("type"));
        model.setContent(res.getString("content"));
      }

    }
    catch(Exception e)
    {
      e.printStackTrace();
    }

      return model;
    }

  //增加新闻
  public int insertNew(News model)
  {

    String new_title=model.getTitle();

    String new_type=model.getType();
    String new_content=model.getContent();

    try
    {
      sql=conn().createStatement();
      String sqlstr="insert into j_new (title,type,content) values('"+new_title
      +"','"+new_type+"','"+new_content+"')";
      sql.executeUpdate(sqlstr);
      sql.close();
      return 1;
    }
    catch(SQLException e)
    {
      e.printStackTrace();
      return 100;
    }
  }

  //新闻修改
  public int Editnew(News model)
  {
    int new_id=model.getId();
    String new_title=model.getTitle();
    String new_type=model.getType();
    String new_content=model.getContent();
    try
    {
      sql=conn().createStatement();
      String sqlstr="update j_new set title='"+new_title+"',type='"+new_type+"',content='"+new_content
      +"' where id='"+new_id+"'";
      sql.executeUpdate(sqlstr);
      sql.close();
      return 1;
    }
    catch(SQLException e)
    {
      e.printStackTrace();
      return 0;
     }
  }

  //删除新闻
  public int deleteNew(int id)
  {
    try
    {
      sql=conn().createStatement();
      String sqlstr="delete from j_new where id='"+id+"'";
      sql.executeUpdate(sqlstr);
      sql.close();
      return 1;
    }
    catch(Exception e)
    {
      e.printStackTrace();
      return 0;
    }

  }

 

}

 

 

//新闻model 类

package com.model;

public class News
{
  private int _id;
  private String _title;
  private String _content;
  private String _type;

  public int getId()
  {
    return _id;
  }

  public void setId(int _id)
  {
    this._id=_id;
  }

  public String getTitle()
  {
    return _title;
  }

  public void setTitle(String _title)
  {
    this._title=_title;
  }

  public String getContent()
  {
    return _content;
  }

  public void setContent(String _content)
  {
    this._content=_content;
  }

    public String getType()
  {
    return _type;
  }

  public void setType(String _type)
  {
    this._type=_type;
  }

}

 

posted @ 2015-05-28 16:32  luoyiming  阅读(672)  评论(0)    收藏  举报