美文网首页
Android设备连接SQL SERVER,向SQL SERVE

Android设备连接SQL SERVER,向SQL SERVE

作者: Anivia_Hanger | 来源:发表于2020-02-24 16:07 被阅读0次

    android设备直连sqlserver需要用到jtds这个jar包,我用的是1.2版本,连接数据库需要用到Connection,插入image的话要用到PreparedStatement,读取需要用到Statement.代码如下

    插入数据:

    Connection sqlConn = DriverManager.getConnection("jdbc:jtds:sqlserver://" + 数据库IP+ "/" + 数据库名称, 数据库账号, 数据库密码);
    
     private void doSaveTest(int imgId, String name, Bitmap image) {
    if(recgImg==null){
    return;
    }
     try {
       byte[] photo_bytes = bitmapToBytes(image);
    //假设Test表字段为(照片编号:int,照片:image,姓名:varchar)
                    PreparedStatement pstmt = sqlConn.prepareStatement("INSERT INTO Test VALUES(? ,? ,? )");
                    pstmt.setInt(1, imgId);
                    pstmt.setBytes(2, image_bytes);
                    pstmt.setString(3,name);
                    pstmt.executeUpdate();
                    pstmt.clearParameters();
                    pstmt.close();
    } catch (SQLException e) {
                e.printStackTrace();
            }
    }
    

    Bitmap二进制转换:

    private byte[] bitmapToBytes(Bitmap bitmap) {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 75, bos);
            byte[] bytes = bos.toByteArray();
            return bytes;
        }
    

    读取图片:

    private Bitmap getPhoto(String imgId) {
            if (sImgId == null) {
                return null;
            }
            Bitmap photo = null;
            Bitmap photoScale = null;
            InputStream input = null;
            String sql = "";
            try {
                sql = "SELECT * FROM Test WHERE imgId = '" + imgId + "'";
                Statement stmt = sqlConn.createStatement();
                ResultSet rs = stmt.executeQuery(sql);
                if (rs.next()) {//<code>ResultSet</code>最初指向第一行  
                    byte[] imgBytes = rs.getBytes("Test_Image");
    
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = 1 + imgBytes.length / 256000;
                    input = new ByteArrayInputStream(imgBytes);
                    SoftReference softRef = new SoftReference(BitmapFactory.decodeStream(input, null, options));
                    photo = (Bitmap) softRef.get();
                    int width = photo.getWidth();
                    int height = photo.getHeight();
                    int startX, startY;
                    int scalWidth, scaleHeight;
                    if ((width > 300) || (height > 300)) {
                        Matrix matrix = new Matrix();
                        if (width > height) {
                            startX = (width - height) / 2;
                            startY = 0;
                            scalWidth = height;
                            scaleHeight = height;
                        } else {
                            startX = 0;
                            startY = 0;
                            scalWidth = width;
                            scaleHeight = width;
                        }
                        final float sx = 300 / (float) scalWidth;
                        final float sy = 300 / (float) scaleHeight;
                        matrix.setScale(sx, sy);
                        photoScale = Bitmap.createBitmap(photo, startX, startY, scalWidth, scaleHeight, matrix, false);
                    }
                }
                rs.close();
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            if (photoScale != null) {
                photo.recycle();
                return photoScale;
            }
            return photo;
        }
    

    相关文章

      网友评论

          本文标题:Android设备连接SQL SERVER,向SQL SERVE

          本文链接:https://www.haomeiwen.com/subject/acwvqhtx.html