如何用my eclipse向my sql中添加和删除数据

2024-11-16 11:45:50
推荐回答(1个)
回答(1):

package jdbchomework;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Scanner;
//操作类文件public class Do {

    //登陆界面
    public boolean found(String username,String password){
        boolean find=false;
        //加载驱动
        try {                        //
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block            e.printStackTrace();
        }
        //建立连接
        Connection cn=null;    //声明连接对象
        PreparedStatement ps=null;  //操作对象
        ResultSet rs=null;  //结果集对象
        
        try {                       //
            cn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Pet","sa","123");
        //    String sql="select * from Admin where userName='"+username+"' and pwd='"+password+"'";
            String sql="select * from Admin where userName=?  and pwd=?";
            ps=cn.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, password);
            rs=ps.executeQuery();
            if(rs.next()){
                find=true;
            }else{
                find=false;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block            e.printStackTrace();
        }
        try{
            if(ps!=null){
                ps.close();
            }
            if(cn!=null){
                cn.close();
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
        return find;
    }
    
    //显示主人信息
    public void show(){
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block            e.printStackTrace();
        }
        //建立连接
        Connection cn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        
        try {
            cn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Pet","sa","123");
        //    String sql="select * from Admin where userName='"+username+"' and pwd='"+password+"'";
            String sql="select * from Admin ";
            ps=cn.prepareStatement(sql);
            rs=ps.executeQuery();
            System.out.println("主人信息列表");
            System.out.println("编号\t姓名\t元宝数");
            while(rs.next()){
                int num=rs.getInt(1);
                String name=rs.getString(2);
                int money=rs.getInt(3);
                System.out.println(num+"\t"+name+"\t"+money);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block            e.printStackTrace();
        }
        try{
            if(rs!=null){
                rs.close();
            }
            if(ps!=null){
                ps.close();
            }
            if(cn!=null){
                cn.close();
            }
        }catch(SQLException e){
            e.printStackTrace();
        }
    }
    
    
    //显示狗狗信息
    public void show1(){
            try {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block                e.printStackTrace();
            }
            //建立连接
            Connection cn=null;
            PreparedStatement ps=null;
            ResultSet rs=null;
            
            try {
                cn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Pet","sa","123");
            //    String sql="select * from Admin where userName='"+username+"' and pwd='"+password+"'";
                String sql="select * from Dog ";
                ps=cn.prepareStatement(sql);
                rs=ps.executeQuery();
                System.out.println("狗狗信息列表");
                System.out.println("编号\t姓名\t健康值\t亲密度\t品种");
                while(rs.next()){
                    int num=rs.getInt(1);
                    String name=rs.getString(2);
                    int health=rs.getInt(3);
                    int love=rs.getInt(4);
                    String strain=rs.getString(5);
                    System.out.println(num+"\t"+name+"\t"+health+"\t"+love+"\t"+strain);
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block                e.printStackTrace();
            }
            try{
                if(rs!=null){
                    rs.close();
                }
                if(ps!=null){
                    ps.close();
                }
                if(cn!=null){
                    cn.close();
                }
            }catch(SQLException e){
                e.printStackTrace();
            }
    }
    //插入数据
    public void show2(String name,int health,int love, String strain ){
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block            e.printStackTrace();
        }
        Connection cn=null;
        PreparedStatement ps=null;
        try {
            cn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Pet","sa","123");
            String sql="insert Dog values(?,?,?,?)";
            ps=cn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setInt(2, health);
            ps.setInt(3, love);
            ps.setString(4, strain);
            int i=ps.executeUpdate();
            if(i>0){
                System.out.println("恭喜你,添加成功!!!");
            }else{
                System.out.println("添加失败!!!");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block            e.printStackTrace();
        }
        
        try {
            if(ps!=null){
                ps.close();
            }
            if(cn!=null){
                cn.close();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block            e.printStackTrace();
        }
    }
    //删除数据
    public void show3(int num){
        //加载驱动
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block            e.printStackTrace();
        }
        Connection cn=null;
        PreparedStatement ps=null;
        try {
            cn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Pet","sa","123");
            String sql="delete Dog where id=?";
            ps=cn.prepareStatement(sql);
            ps.setInt(1, num);
            int i=ps.executeUpdate();
            if(i>0){
                System.out.println("恭喜你,删除成功!!!");
            }else{
                System.out.println("删除失败,你输入的ID不存咋!!!");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block            e.printStackTrace();
        }
        //释放连接
        try {
            if(ps!=null){
                ps.close();
            }
            if(cn!=null){
                cn.close();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block            e.printStackTrace();
        }
    }
}