package Dao; import java.sql.*; public class News { private Connection con=null; private Statement st=null; private PreparedStatement ps=null; private ResultSet rs=null; public final static String driver="com.mysql.jdbc.Driver"; public final static String url="jdbc:mysql://localhost:3306/News"; public final static String name="root"; public final static String password="123456"; //建立数据库连接 public void getConn(){ try{ Class.forName(driver); //加载驱动类 con=DriverManager.getConnection(url, name, password); System.out.println("数据库已连接"); } catch (ClassNotFoundException e) { // TODO: handle exception System.out.println("驱动类未找到"+e.toString()); } catch (SQLException e) { // TODO: handle exception System.out.println("连接对象创建失败"+e.toString()); } } //发布新闻 public int insertUser(String title,String nid,String content,String time){ int i=0; String sql="insert into news(title,nid,content,time) values (?,?,?,?)"; try{ ps=con.prepareStatement(sql); ps.setString(1, title); ps.setString(2, nid); ps.setString(3, content); ps.setString(4, time); i=ps.executeUpdate(); } catch (SQLException e) { // TODO: handle exception e.printStackTrace(); } return i; } //查询新闻编号 public boolean findID(int uid){ boolean flag=false; String sql="select * from news where uid=?"; try{ ps=con.prepareStatement(sql); ps.setInt(1,uid); rs=ps.executeQuery(); if(rs.next()){ System.out.println("uid存在"); flag=true; } else{ System.out.println("uid不存在"); flag=false; } } catch (SQLException e) { // TODO: handle exception e.printStackTrace(); } return flag; } //查询新闻所有信息 public ResultSet findAll(){ String sql="select * from news"; try{ st=con.createStatement(); rs=st.executeQuery(sql); } catch (SQLException e) { // TODO: handle exception e.printStackTrace(); } return rs; } //删除新闻 public void delete(int uid){ String sql="delete from news where uid=? "; try{ ps=con.prepareStatement(sql); ps.setInt(1,uid); ps.executeUpdate(); } catch (SQLException e) { // TODO: handle exception e.printStackTrace(); } } //修改新闻 public void Update(int uid,String title,String nid,String content,String time){ String sql="update news set title=?,nid=?,content=?,time=? where uid=?"; try{ ps=con.prepareStatement(sql); ps.setString(1, title); ps.setString(2, nid); ps.setString(3, content); ps.setString(4, time); ps.setInt(5, uid); ps.executeUpdate(); } catch (SQLException e) { // TODO: handle exception e.printStackTrace(); } } }

qwqwx585160 LV2
2024年12月15日
wzd031105 LV2
2024年12月6日
lmj12345 LV2
2024年8月19日
cong58cong LV8
2024年6月26日
bankroll LV5
2024年6月13日
Waner111 LV1
2024年1月4日
asddwh LV13
2023年12月26日
zj20020510 LV5
2023年9月24日
monolog LV4
2023年4月18日
whq12138 LV3
2023年1月6日