In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail how to practice the new features of MySQL implementing JDBC 2.0 swap 3.0. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Yesterday a day did not debug the program to upload images to the database, although checked the CSDN, but the information is not good. Today, I think of several key points that I haven't tried carefully, mainly whether the new features of JDBC API have any effect on MySql, and if not, why are there new API methods in the ResultSet class in the MySQL Driver package? In addition, several classes of the upload package determine whether it is possible to add images to the database. Whether it's okay or not, I'll have to try it again. Because yesterday I tested and passed the new update method of JDBC API, so I plan to debug the program of uploading images to the database first today. After a short period of debugging, I finally passed and called the image display program to try, the image came out, and I tried the image in jpg format. It was a great pleasure. The successful debugging of the image saving function of the database has increased my confidence in debugging the new plug-in function of JDBC API, and it has been successful after several twists and turns. In order to remember this process, I would like to write it down.
Original goal: to save the client-side gif or jpg image files to the server-side MySQL database through the network.
Design idea: make a form for customers to use, through which customers can find the client image file, and extract the file name of the file in the form input box. After the customer submits the form, the insertpic.jsp on the server side will be activated and the data of the image file will be saved to the database through insertpic.jsp.
In the process of implementation, we have encountered and overcome new problems: how to use JDBC's new API insert, update and delete instructions; how to use upload package, learn and demonstrate the connection pool and image display program provided in "Java Servlets programming Guide".
Specific programming ideas, problems encountered, summary:
1. Database table name: javadoc; saves image data in fields of Blob or MediumBlob or LongBlob type.
The SQL statement to create the data table is as follows:
SQL = "CREATE TABLE javadoc (" +)
"gid int (11) NOT NULL auto_increment," +
"gheading varchar," +
"gimage longblob," +
"PRIMARY KEY (gid))"
2. The input box on the form to find and extract the image file name is implemented with the following statement:
Image
3. Of the form
To add the following properties and property values:
Enctype= "multipart/form-data"
After this attribute is added, the form can no longer be used as a form to transfer other data fields at the same time, such as string fields.
4. Enable upload package containing File.class, Files.class, Request.class, SmartUpload.class and SmartUploadException.class to help save images and reduce the difficulty of development.
The original code of several classes can be found in the relevant files, and the common methods of the File class have been glued to the related jsp files.
5. JDBC's new API insert, update and delete functions are as follows:
To insert a record:
(1) prepare an empty record: MoveToInsertRow ()
(2) enter data: updateXXX ()
(3) implement insertion: insertRow ()
Update a record step:
(1) Positioning recording: using absolute () or relative () and other methods
(2) enter the new value: updateXXX ()
(3) implement the update: updateRow ()
To delete a record:
(1) Positioning recording: using absolute () or relative () and other methods
(2) implement deletion: deleteRow ()
6. Problems encountered in inserting data: I designed an automatically incremental gid field in the table that saved the image, and defined it as NOT NULL. In the case where a new value is usually inserted with a SQL statement, the interpolation of the field can be ignored and the database will automatically assign the value. However, in the process of inserting a record using JDBC API, it is found that this field cannot be ignored, and rs.updateLong ("gid"..) must be called. Assign a value, otherwise the system will have an exception. I also thought and tried to apply for a cursor that does not contain this field, and then perform the above steps to insert a record on the cursor, but there is also an exception. In addition, I have a gheading field in my table, and the default value of this field is not defined. It is possible to create a cursor that does not contain this field and then insert a new record, that is, to perform an insert operation that can be ignored for fields like gheading. From this, I infer that as long as the field is described as NOT NULL, you can't ignore it when you program to insert records with JDBC's new API. You must assign it explicitly, otherwise the system will have an exception.
7. How to assign a new value to an automatically incremented field when you program to insert a record?
My plan is as follows:
(1) use select * form tablename order by gid to apply for a cursor, where gid is an automatic increment field and cannot be a NULL field. For simplicity, assume that the cursor will not be empty.
(2) last () the cursor to the bottom of the cursor
(3) assign the value of the gid field to a temporary variable mid
(4) mid++
(5) implement updateLong ("gid", mid) for the gid field
...
8. What are the problems to be considered in the programming of inserting records for automatically incremental fields to assign new values according to the above steps?
I think we should consider locking the record, otherwise there are still potential problems with exceptions. This is the end of the practice on how to implement the new features of JDBC 2.0 JDBC 3.0 in MySQL. I hope the above content can be of some help and learn more. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.