الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

خطأ في كود عمل insert للblob

بدأه maas في 4 يوليو 2010 · 3 رد · 512 مشاهدة · في JavaEE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

يا شباب انا أحاول ان أعمل insert لfile upload في الjsp

و هذا هو الكود و لكن الخطأ يظهر Null Pointer Exception

 String cname = request.getParameter("cname");
  String description = request.getParameter("description");
  String support = request.getParameter("support");
  String attachment=request.getParameter("attachment");
  String[] userlist =request.getParameterValues("userlist");
  String notify = request.getParameter("notify");
  String vendorname = request.getParameter("vendorname");
  String from_dt = request.getParameter("from_dt");
  String to_dt = request.getParameter("to_dt");
  File fileNm=null;

  if(attachment!=null)
		{
	    	fileNm = new File(attachment);
		}

  String connectionURL = "jdbc:oracle:thin:@X.X.X.XX:XXX:test";

  Connection conn=null;

  PreparedStatement ps=null;

  Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();


  if (cname != null ){
  try
  {
  	conn = DriverManager.getConnection(connectionURL, "test", "test");

  	for (int i=0; i<userlist.length; i++){ 

  	FileInputStream fis = new FileInputStream(fileNm);

     	String queryString = "INSERT INTO contract_detail(contract_name,description,support,c_from,c_to,userlist,notify,vendorname,attachment) VALUES (?,?,?,TO_DATE(?,'YYYY/MM/DD'),TO_DATE(?,'YYYY/MM/DD'),?,?,?,?)";

     ps = conn.prepareStatement(queryString);  

	 ps.setString(1, cname);
	 ps.setString(2, description);
	 ps.setString(3, support);
	 ps.setString(4, from_dt);
	 ps.setString(5, to_dt);
	 ps.setString(6, userlist);
	 ps.setString(7, notify);
	 ps.setString(8, vendorname);
	 ps.setBinaryStream(9,fis,(int)fileNm.length());


	 ps.executeUpdate();      
	 fis.close();
     }       
	 out.println(" The Contract is updated successfully");

  }

أرجو المساعدة في حل هذه المشكلة

#2

تأكد أولا أنك وضعت ال driver الخاص ب oracle في المجلد WEB-INF/lib

لعمل ال upload، إستعمل مكتبة commons-fileupload. هذا مثال على استعمالها /index.php?showtopic=212879

مثال لتخزين ملفات في قاعدة بيانات /index.php?showtopic=214963

#3

شكرا أخي الكريم على المساعدة

هل الكود الذي وضعته أنا خاطىء؟أم يحتاج إلى تعديل؟

قرأت المثال الأول و الثاني من الذين وضعتهم

و لكن مشكلتي أنني مبتدىء ... و اخترت أن أطبق المثال الثاني

و لكن فشلت

#4

استخدم هذا الكود

public void saveblob() throws IOException, SQLException {

int rows = 0;

FileInputStream fin = null;

OutputStream out = null;

ResultSet rslt = null;

Statement stmt = null;

BLOB photo = null;

try {

confinger.setAutoCommit(false);

stmt = confinger.createStatement();

rslt = stmt.executeQuery("SELECT myblob FROM table WHERE no ='1' FOR UPDATE NOWAIT");

while (rslt.next()) {

rows++;

photo = (BLOB)rslt.getBlob("myblob");

}

rslt.close();

rslt = null;

/**

* If it doesn't exist, then insert a row in the information table

* This creates the LOB locators

*/

if (rows == 0) {

formatter = new SimpleDateFormat("dd/MM/yyyy");

rows = stmt.executeUpdate("INSERT INTO table VALUES ('1',empty_blob()");

// Retrieve the locator

rows = 0;

rslt = stmt.executeQuery("select myblob FROM table WHERE no ='1' FOR UPDATE NOWAIT");

rslt.next();

photo = ((OracleResultSet)rslt).getBLOB("myblob");

rslt.close();

rslt = null;

}

stmt.close();

stmt = null;

File binaryFile = new File("systems/timagf1");

fin = new FileInputStream(binaryFile);

out = photo.getBinaryOutputStream();

// Get the optimal buffer size from the BLOB

byte[] buffer = new byte[photo.getBufferSize()];

int length = 0;

while ((length = fin.read(buffer)) != -1) {

out.write(buffer, 0, length);

}

out.close();

out = null;

fin.close();

fin = null;

confinger.commit();

try {

} catch (Exception ex) {

}

}

catch (SQLException e) {

System.err.println("SQL Error: " + e.getMessage());

}

catch (IOException e) {

System.err.println("IO Error: " + e.getMessage());

}

finally {

if (rslt != null) {

try {

rslt.close();

}

catch (SQLException ignore) {}

}

if (stmt != null) {

try {

stmt.close();

}

catch (SQLException ignore) {}

}

if (out != null) {

try {

out.close();

}

catch (IOException ignore) {}

}

if (fin != null) {

try {

fin.close();

}

catch (IOException ignore) {}

}

}

}

مواضيع مشابهة