Share very useful Java programs (key code) (i)

Source: Internet
Author: User

Original: Sharing very useful Java programs (key code) (i)


Share some very useful Java programs (key code) and hope to help you.

1. Get the name of the current method


2. Turn string to date

Or is:


3. Using JDBC to link Oracle

public class Oraclejdbctest {String Driverclass = "Oracle.jdbc.driver.OracleDriver";          Connection con;       public void init (FileInputStream fs) throws ClassNotFoundException, SQLException, FileNotFoundException, IOException           {Properties props = new Properties ();           Props.load (FS);           String url = props.getproperty ("Db.url");           String userName = Props.getproperty ("Db.user");           String Password = props.getproperty ("Db.password");              Class.forName (Driverclass);       Con=drivermanager.getconnection (URL, userName, password); } public void Fetch () throws SQLException, IOException {preparedstatement PS = Con.preparestatem           ENT ("Select Sysdate from Dual");              ResultSet rs = Ps.executequery ();           while (Rs.next ()) {//Does the thing you do} rs.close ();       Ps.close (); } public static void Main (string[] ArGS) {oraclejdbctest test = new Oraclejdbctest ();           Test.init ();       Test.fetch ();    }


4. Util Java. Date turns into SQL. Date

Java.util.Date utildate = new Java.util.Date ();   


5. Create a thumbnail image of a picture

private void Createthumbnail (string filename, int thumbwidth, int thumbheight, int quality, string outfilename) Throws Interruptedexception, FileNotFoundException, IOException {//load image from filename Im           Age image = Toolkit.getdefaulttoolkit (). getImage (filename);           Mediatracker mediatracker = new Mediatracker (new Container ());           Mediatracker.addimage (image, 0);           Mediatracker.waitforid (0);              Use the this to test for errors in this point:System.out.println (Mediatracker.iserrorany ());           Determine thumbnail size from WIDTH and HEIGHT double thumbratio = (double) thumbwidth/(double) thumbheight;           int imagewidth = Image.getwidth (null);           int imageheight = Image.getheight (null);           Double imageratio = (double) imagewidth/(double) imageheight;           if (Thumbratio < imageratio) {thumbheight = (int) (thumbwidth/imageratio);           } else {    Thumbwidth = (int) (thumbheight * imageratio);           }//Draw original image to thumbnail image object and//scale it to the new size On-the-fly           BufferedImage thumbimage = new BufferedImage (Thumbwidth, Thumbheight, Bufferedimage.type_int_rgb);           Graphics2D graphics2d = Thumbimage.creategraphics ();           Graphics2d.setrenderinghint (Renderinghints.key_interpolation, renderinghints.value_interpolation_bilinear);              Graphics2d.drawimage (image, 0, 0, thumbwidth, thumbheight, NULL); Save thumbnail image to outfilename bufferedoutputstream out = new Bufferedoutputstream (New FileOutputStream (           Outfilename));           JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);           JPEGEncodeParam param = Encoder.getdefaultjpegencodeparam (thumbimage);           Quality = Math.max (0, Math.min (quality, 100));           Param.setquality ((float) quality/100.0f, false); Encoder.setjpegencodeparam (PAram);           Encoder.encode (Thumbimage);    Out.close ();


6. Generate PDF using Itext jar


follow-up will continue to share with you some useful code snippets, so stay tuned. --Hurry

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Share very useful Java programs (key code) (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.