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

ادخال الصور في Sql Server 2005 من خلال كود جافا

بدأه لونه2010 في 9 نوفمبر 2008 · 11 رد · 1,272 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

بســم الله الـرحمــن الرحيــم

ارجو من لديه خبرة في الجافا و

sql server 2005

فقط توضيح طريقة ادراج الصور في جدول عملناه في

sql

طبعا من خلال الجافا

#2

السلام عليكم

بصراحة بعد بحث مضني

وجدت لك هذه الوصلة

http://www.roseindia.net/jdbc/save_image.shtml

المشكلة أنها تتحدث عن MySql

أضعها لك لعل وعسى

برجاء إفادتي إن حصلتي على نتيجة

بصراحة لم أستطع التجربة لأني لا أمتلك sqlServer

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#3

اذا كنت قد جهزت الجداول ب sql لاستقبال نمط معطيات من نوع image


2> Player_Id int IDENTITY (1, 1) NOT NULL ,
3> Photograph image NULL
4> )
5> GO
create table Players (

كل ما بقي عليك هوا عملية الاتصال بقاعدة البيانات ..مثال علاء يفيد لو كان بالmysql لكن فقط اعرف ما هي driver المناسبة


String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:scorpian";
String username = "userName";
String password = "pass";
Class.forName(driver); // load Oracle driver
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
public static Connection getOracleConnection() throws Exception {

بالتوفيق ..

الله معك..

#4

شادي لاحظ أنها تتكلم عن SqlServer

وليس Oracle

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#5

المشكلة ان سؤالها متشعب

بالبداية تقول انها تريد ربط sql server ثم تقول ان الجداول مشكلة بالـ sql

على كل حال ارجو ما قدمناه قد يفيد ...الا اذا كنت انا على خطأ ما والله اعلم ...

بالتوفيق

الله معك..

#6

المشكلة شادي أن Sql ليست موحدة للجميع

على ما أظن في SqlServer

فإن صورة هي Image

لكني غير متأكد لهذا نبهتك

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#7

السلام عليكــم ورحمـة الله وبركاتــة

الاخوة الافاضل شكرا جزيلا على تعاونكم

اريد توضيح نقطة مهمة

1-انشأت جدول من خلال اسكيوال

وخليت احد الأعمدة من نوع

image

لتهيئتها لإدخال صورة لكني لم ادرج فيه اي صفوف ولا صور

اريد ان يتم هذا من خلال كود جافا

2-هبأت الداتا بيس الي انشأت فيها الجدول للتعامل معها من خلال الجافا

اقصد اتممت عمل خطوات ربط الجافا ب اسكيوال سيرفر 2005

3-بقي فقط عمل كود في الجافا لتعبئة الجدول ( اي ادخال الصور)

رقم 3 فقط هو المطلوب

اتمنى انه الصورة وضحت

وشكرا

#8

جيد ..

هل حاولتي بالمثال الذي قدمه الاخ علاء ...

تحياتي

الله معك..

تم تعديل هذه المشاركة بواسطة shado في 10 نوفمبر 2008 في 18:59

#9

هذا مثال صغير لعملية inserte date بقاعدة بيانات sql من خلال كود جافا



public class InsertSuppliers {

public static void main(String args[]) {

String url = "jdbc:mySubprotocol:myDataSource";
Connection con;
Statement stmt;
String query = "select SUP_NAME, SUP_ID from SUPPLIERS";

try {
Class.forName("myDriver.ClassName");

} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

try {
con = DriverManager.getConnection(url,
"myLogin", "myPassword");

stmt = con.createStatement();

stmt.executeUpdate("insert into SUPPLIERS " +
"values(49, 'Superior Coffee', '1 Party Place', " +
"'Mendocino', 'CA', '95460')");

stmt.executeUpdate("insert into SUPPLIERS " +
"values(101, 'Acme, Inc.', '99 Market Street', " +
"'Groundsville', 'CA', '95199')");

stmt.executeUpdate("insert into SUPPLIERS " +
"values(150, 'The High Ground', '100 Coffee Lane', " +
"'Meadows', 'CA', '93966')");

ResultSet rs = stmt.executeQuery(query);

System.out.println("Suppliers and their ID Numbers:");
while (rs.next()) {
String s = rs.getString("SUP_NAME");
int n = rs.getInt("SUP_ID");
System.out.println(s + " " + n);
}

stmt.close();
con.close();

} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}
import java.sql.*;

فقط قومي بتغير معلومات انماط المعطيات والقيود على الجداول

بالتوفيق

الله معك..

#10

شكرا جزيلا

زبط معاي كود الاخ علاء

الان فقط احتاج كودجافا لعرض الصور

لاني لما اعمل select

من sql ما تطلع لي الصور لأنها binary

اخ شدو اشكرك على مساعداتك

جزاكم الله خيرا

#11

هذه الفقرة نسختها لك من ملف المساعدة الخاص بjdbc

Large Value Data Types

In earlier versions of SQL Server, working with large-value data types required special handling. Large-value data types are those that exceed the maximum row size of 8 KB. SQL Server 2005 introduces a max specifier for varchar, nvarchar, and varbinary data types to allow storage of values as large as 2^31 bytes. Table columns and Transact-SQL variables can specify varchar(max), nvarchar(max), or varbinary(max) data types.

The primary scenarios for working with large-value types involve retrieving them from a database, or adding them to a database. The following sections describe different approaches to accomplish these tasks.
Retrieving Large-Value Types from a Database

When you retrieve a non-binary large-value data type—such as the varchar(max) data type—from a database, one approach is to read that data as a character stream. In the following example, the executeQuery method of the SQLServerStatement class is used to retrieve data from the database and return it as a result set. Then the getCharacterStream method of the SQLServerResultSet class is used to read the large-value data from the result set.

ResultSet rs = stmt.executeQuery("SELECT TOP 1 * FROM Test1");
rs.next();
Reader reader = rs.getCharacterStream(2);

Note: This same approach can also be used for the text, ntext, and nvarchar(max) data types.

When you retrieve a binary large-value data type—such as the varbinary(max) data type—from a database, there are several approaches that you can take. The most efficient approach is to read the data as a binary stream, as in the following:

ResultSet rs = stmt.executeQuery("SELECT photo FROM mypics");
Rs.next();
InputStream is = rs.getBinaryStream(2);

You can also use the getBytes method to read the data as a byte array, as in the following:

ResultSet rs = stmt.executeQuery("SELECT photo FROM mypics");
Rs.next();
byte [] b = rs.getBytes(2);

Note: You can also read the data as a BLOB. However, this is less efficient than the two methods shown previously.

Adding Large-Value Types to a Database

Uploading large data with the JDBC driver works well for the memory-sized cases, and in the larger-than-memory cases, streaming is the primary option. However, the most efficient way to upload large data is through the stream interfaces.

Using a string or bytes is also an option, as in the following:

PreparedStatement pstmt = con.prepareStatement("INSERT INTO test1 (c1_id, c2_vcmax) VALUES (?, ?)");
pstmt.setInt(1, 1);
pstmt.setString(2, htmlStr);
pstmt.executeUpdate();

Note: This approach can also be used for values that are stored in text, ntext, and nvarchar(max) columns.

If you have an image library on the server and must upload entire binary image files to a varbinary(max) column, the most efficient method with the JDBC driver is to use streams directly, as in the following:

Statement stmt = con.createStatement();
PreparedStatement pstmt = con.prepareStatement("INSERT INTO test1 
VALUES( ?, ? )");
FileReader in = new FileReader(new File("CLOBFile20mb.txt");
long len = fileIn.length();
int id = 1;
pstmt.setInt(1,id);
pstmt.setCharacterStream(2, in, (int)len);
pstmt.executeUpdate();
in.close();

المشكلة أنه ليس لدي SQLServer لأجرب

حاولي وانظري ما آخر شيء ستصلي إليه

وبإذن الله سنرى حلاً

وأتمنى إن وصلتي لشيء أن تخبرينا

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#12

السلام عليكــم ورحمـة الله وبركاتــه

تم عمل الكود ولله الحمد كالتالي :

import java.sql.*;
import java.io.*;


public class saveImage 
{
		public static void main(String[] args) 
		{
				DB db = new DB();
				Connection conn=db.dbConnect(
				 "jdbc:sqlserver://localhost:1433;DatabaseName=mayada","noha","123");
				db.insertImage(conn);
				db.getImageData(conn);

		}

}

class DB
{


		public DB() {}

		public Connection dbConnect( String db_connect_string,
		   String db_userid, String db_password)
		{
				try
				{
						Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
						Connection conn = DriverManager.getConnection(
						  db_connect_string, db_userid, db_password);

						System.out.println("connected");
						return conn;

				}
				catch (Exception e)
				{
						e.printStackTrace();
						return null;
				}
		}

		public void insertImage(Connection conn)
		{
				int len;
				String query;
				PreparedStatement pstmt;



				try
				{


						query = ("insert into dbo.images VALUES(?,?,?)");
					   File file = new File("C:/Users/RMC/Desktop/images11/1.jpg");
					   FileInputStream fis = new FileInputStream(file);
						pstmt = conn.prepareStatement(query);
						pstmt.setString(1,"mayada");
						pstmt.setString(3,"qq");
						len = (int)file.length();
						// Method used to insert a stream of bytes
						pstmt.setBinaryStream(2, fis, len); 
						pstmt.executeUpdate();


				}
				catch (Exception e)
				{
						e.printStackTrace();
				}
		}



		public void getImageData(Connection conn)
		{

				 byte[] fileBytes;
				 String query = "select [image name] from dbo.images ";
				 try
				 {				   
						 Statement state = conn.createStatement();
						 ResultSet rs = state.executeQuery(query);
						 if (rs.next())
						{
								  fileBytes = rs.getBytes(1);
								  FileOutputStream targetFile=new FileOutputStream("c://filepath//new.JPG");
								  targetFile.write(fileBytes);
								  targetFile.close();
						} 



				 }
				 catch (Exception e)
				 {
						 e.printStackTrace();
				 }
		}



};

شكرا لكل من ساعدني

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