无法使用android在ms sql数据库中插入记录

时间:2015-11-03 13:45:23

标签: android image activerecord ftp insert

我正在尝试使用ftp将图像上传到web文件夹并且它工作正常但是当我尝试插入相应图像的记录时它不起作用并且程序在尝试阻止之前跳转。我用过每个循环上传图像以及获取图像的文件名。以下是用于插入和上传图像的代码。   因此,当我尝试为每个循环上传超过10个图像时,我无法插入记录,但ftp图像上传工作正常。   我需要知道我在哪里犯了错误,任何人都可以告诉我我在哪里做错了。

public class DisplayImageActivity extends Activity{
private ImageAdapter imageAdapter;
String call, db, un, passwords;
String orig;
String names;
String icore;
String imagename;
Connection con;
ResultSet rs;
ConnectionClass connectionclass;
String imagenametag;
@SuppressLint("NewApi")
private Connection ConnectionHelper(String user, String password,
                                    String database, String server) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);
    Connection connection = null;
    String ConnectionURL = null;
    try {
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        ConnectionURL = "jdbc:jtds:sqlserver://" + server + ";"
                + "databaseName=" + database + ";user=" + user
                + ";password=" + password + ";";
        connection = DriverManager.getConnection(ConnectionURL);
    } catch (SQLException se) {
        Log.e("ERRO", se.getMessage());
    } catch (ClassNotFoundException e) {
        Log.e("ERRO", e.getMessage());
    } catch (Exception e) {
        Log.e("ERRO", e.getMessage());
    }
    return connection;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gridview);
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri
            .parse("file://"
                    + Environment.getExternalStorageDirectory())));
    connectionclass = new ConnectionClass();
    call = connectionclass.getip();
    un = connectionclass.getun();
    passwords = connectionclass.getpassword();
    db = connectionclass.getdb();

//    connect = ConnectionHelper(un, passwords, db, call);
    GridView gridview = (GridView) findViewById(R.id.gridview);
    imageAdapter = new ImageAdapter(this);
    gridview.setAdapter(imageAdapter);
    String ExternalStorageDirectoryPath = Environment
            .getExternalStorageDirectory()
            .getAbsolutePath();
    String targetPath = ExternalStorageDirectoryPath + "/JCG Camera";
    Toast.makeText(getApplicationContext(), targetPath, Toast.LENGTH_LONG).show();
    File targetDirector = new File(targetPath);
    File[] files = targetDirector.listFiles();



        orig = "/site1/Image/";

    for(File file1:files){
        imageAdapter.add(file1.getAbsolutePath());
        if(!file1.isFile())continue;
        String msg = "";
        String[]bits=file1.getName().split("_");
      //  imagenametag = file1.getName();
       // String[] parts = imagenametag.split("_");
        names = bits[0];
        icore = bits[1];
        imagename = file1.getName();
        try {
            con = ConnectionHelper(un, passwords, db, call);
            if (con == null) {
                msg = "Error in connection with SQL server";
            } else {
                String commands = "Insert into Image(Image_Tag,Image_Name,orig_img_path) values ('" + names + "','" + imagename + "','" + orig + "')";
                PreparedStatement preStmt = con.prepareStatement(commands);
                preStmt.executeUpdate();
                msg = "Info Saved";

                    new FTPFileUpload(this).execute(
                            FTPConstants.SERVER,
                            FTPConstants.USER_NAME,
                            FTPConstants.PASSWORD,
                            file1.getAbsolutePath(),
                            "/site1/Image/" + file1.getName());
            }
        } catch (SQLIntegrityConstraintViolationException ec) {
            msg = "Image Taken Already";
        } catch (IOError ex) {
            // TODO: handle exception
            msg = ex.getMessage().toString();
            Log.d("skool", msg);
        } catch (AndroidRuntimeException ex) {
            msg = ex.getMessage().toString();
            Log.d("skool", msg);

        } catch (NullPointerException ex) {
            msg = ex.getMessage().toString();
            Log.d("skool", msg);
        } catch (Exception ex) {
            //    msg= ""+spstud.getSelectedItem()+"'s Record has been saved already for"+date.getText().toString()+"";
            Log.d("skool", msg);
        }
        Toast.makeText(DisplayImageActivity.this, msg, Toast.LENGTH_LONG).show();
    }
 }

@Override
public void onBackPressed() {
    super.onBackPressed();
    String ExternalStorageDirectoryPath = Environment
            .getExternalStorageDirectory()
            .getAbsolutePath();

    String targetPath = ExternalStorageDirectoryPath + "/JCG Camera";

  //  Toast.makeText(getApplicationContext(), targetPath, Toast.LENGTH_LONG).show();
    File targetDirector = new File(targetPath);

    File[] files = targetDirector.listFiles();
    for (File file : files) {
        file.delete();

    }
    Intent i=new Intent(DisplayImageActivity.this,ListMain.class);
    startActivity(i);
    DisplayImageActivity.this.finish();
}

}

0 个答案:

没有答案