Java链接数据库读取、存储基本操作

Java连接数据库

前注:sql字符串中,要向数据库中插入 字符串、日期,其两边是需要加上单引号的

向数据库中新建一个表:

try {
            DataBase database=new DataBase();
            database.connectSqlServer();
            
            String sql="DROP TABLE thingbase;"
                    +" CREATE TABLE thingbase(Thing_ID  CHAR(17) NOT NULL UNIQUE,"  //物品编号
                                            + "Name      CHAR(30) NOT NULL,"//物品名称
                                            + "Price     MONEY NOT NULL ,"//物品价格 
                                            + "Note      CHAR(50),"//物品备注
                                            + "Picture   IMAGE NOT NULL,"//物品图片
                                            + "PRIMARY KEY(Thing_ID));";
            PreparedStatement thingTable=DataBase.connection.prepareStatement(sql);
            //thingTable.setString(1, "Number  INTEGER");
            //thingTable.setString(2, "Name    CHAR(30) NOT NULL");
            //thingTable.setString(3, "Price   NOT NULL DOUBLE");
            //thingTable.setString(4, "Note    CHAR(50)");
            //thingTable.setString(5, "Picture IMAGE NOT NULL");
            thingTable.execute();//执行
            thingTable.close();
            System.out.println("创建表thingbase成功");
            
        } catch (Exception ex) {
            System.out.println("连接数据库失败");
            Logger.getLogger(CreateTable.class.getName()).log(Level.SEVERE, null, ex);
        }

注意:新建时不能使用PreparedStatement类中的?进行提前占位

连接并注册数据库代码

String url="jdbc:sqlserver://LAPTOP5FN1E5JD;integratedSecurity=true;database=SharedPlatFrom";
String name="com.microsoft.sqlserver.jdbc.SQLServerDriver";

    public void connectSqlServer() throws Exception{
        //注册驱动
        Class.forName(name);
        System.out.println("找到驱动类");
        connection=(Connection)DriverManager.getConnection(url);
        System.out.println("SQLServer连接成功:"+connection);
    }
    

使用PraparedStatement向已连接数据库中指定的表录入数据

(可使用?提前进行占位进行预编译,随后使用.set+“/*录入数据类型*/”(/*位置*/,/*需要录入的数据*/)进行操作)

        try{
            File file =thing.getPicture();
            byte[] b = new byte[(int) file.length()];
            FileInputStream fis = new FileInputStream(file);
            fis.read(b);
            String sql = "INSERT INTO thingbase(thing_id,name,price,note,picture) VALUES(?,?,?,?,?)";
            try (PreparedStatement pStatement = connection.prepareStatement(sql)) {
                pStatement.setString(1, thing.getThing_id());
                pStatement.setString(2, thing.getName());
                pStatement.setDouble(3, thing.getPrice());
                pStatement.setString(4, thing.getNote());
                pStatement.setBytes (5, b);
                pStatement.execute();//执行
            }
        }catch (FileNotFoundException e) {
            System.out.println("文件未赋值");
            e.printStackTrace();
        }catch (IOException ex) {
            Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
        }catch (SQLException ex) {
            Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
        } 
        System.out.println("上传成功");
        JOptionPane.showMessageDialog(null,"上传成功","通知",JOptionPane.PLAIN_MESSAGE);

使用PreparedStatement从已连接数据库指定的表中读取数据

        Information thing=new Information();
        try {
            Statement statement = connection.createStatement();
            String Query="SELECT * FROM thingbase WHERE"+where;
            ResultSet rs = statement.executeQuery(Query);
            byte[] bb ;
            while (rs.next()) {
                File file = new File("e:\\a3.jpg");
                bb = rs.getBytes(5);//通过列号得到
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(bb);
                thing.setPicture(file);
                thing.setThing_id(rs.getString(1));
                thing.setName(rs.getString(2));
                thing.setPrice(rs.getDouble(3));
                thing.setNote(rs.getString(4));
                System.out.println("数据读取完成");
                new TestMain().init(thing);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值