国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

通過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.Word=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)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 恭城| 运城市| 新巴尔虎左旗| 平定县| 当涂县| 宁乡县| 金华市| 东安县| 武安市| 辽宁省| 绥中县| 修水县| 浏阳市| 华阴市| 大宁县| 福清市| 平湖市| 隆化县| 松江区| 中西区| 永寿县| 塔河县| 浪卡子县| 永福县| 乐亭县| 永昌县| 土默特左旗| 锦州市| 淄博市| 溆浦县| 抚顺市| 宁晋县| 佛教| 和静县| 武功县| 东山县| 湟中县| 盘锦市| 洪泽县| 慈溪市| 慈溪市|