首页 > 学院 > 开发设计 > 正文

通过my Eclipse控制台向数据库(SQL2008)中查找、删除、插入信息

2019-11-14 22:37:12
字体:
来源:转载
供稿:网友
通过my Eclipse控制台向数据库(SQL2008)中查找、删除、插入信息
如果编译程序有什么错误还望大家多多指正代码执行所需数据库、架包及java源文件已上传至文件   文件名 SQl_JDBC.zip用my Eclipse控制台操作数据库之前(SQL 2008)之前 应先引入一个架包(sqljdbc4.jar)  在架包导入之后才能通过控制台操作数据库
com.microsoft.sqlserver.jdbc.SQLServerDriver  在引入架包(sqljdbc4.jar)之后的驱动程序
jdbc:sqlserver://localhost:1433;databaseName=Pet","sa","123"    jdbc:sqlServer:   固定写法    localhost  本地数据   1433代表SQL的数据端口   databaseName=数据库名   sa为SQL数据库登录名   123为SQL数据库登录密码
package jdbchomework;import java.util.Scanner;//测试类文件public class test {    //主程序入口    public static void main(String[] args) {        //实例化键盘输入        Scanner input=new Scanner(System.in);        //实例化操作类        Do a=new Do();        System.out.PRint("请输入用户名:");        String username=input.next();        System.out.print("请输入密码");        String passWord=input.next();        if(a.found(username, password)){    //输入用户名及密码与数据库中的用户名及密码作比较            System.out.println("登陆成功!欢迎你!"+username);for(;;){          System.out.println("请选择操作:")                System.out.println("1.查询管理员信息  2.查询狗狗信息  3.添加狗狗  4.删除狗狗  0.退出");                System.out.print("请选择:");                int num=input.nextInt();                switch(num){                    case 1:                        a.show();  //调用操作类  显示主人信息                        break;                    case 2:                        a.show1();  //调用操作类  显示狗狗信息                        break;                    case 3:                        System.out.print("请输入狗狗的姓名:");                        String name=input.next();                        System.out.print("请输入狗狗的健康值:");                        int health=input.nextInt();                        System.out.print("请输入与狗狗的亲密度:");                        int love=input.nextInt();                        System.out.print("请输入狗狗的品种:");                        String strain=input.next();                        a.show2(name, health, love, strain);   //调用操作类  插入数据  向数据库Dog中插入数据                        break;                    case 4:                        System.out.print("请输入编号:");                        int num1=input.nextInt();                        a.show3(num1);     //调用操作类  删除数据   删除数据库Dog表中id为num1的数据                        break;                    case 0:                        num=0;                        break;                    default:                                System.out.println("输入错误");                        break;                }                if(num==0){                    System.out.println("系统退出,谢谢使用!!!");                    break;                }            }                    }else{            System.out.println("用户名或者密码错误");        }            }}
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();        }    }}


上一篇:Spring AOP

下一篇:Java日志设计&实践(3)

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表