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)