Monthly Archives: February 2018

Redirecting System.out to JTextPane

Questions: I have a class (shown below) that extends JPanel and contains a JTextPane. I want to redirect System.out and System.err to my JTextPane. My class does not seem to work. When I run it, it does redirect the system prints, but they do not print to my JTextPane. Please help! Note: The calls are… Read More »

Java Runtime.exec()

Questions: I can run this command from the command line without any problem (the validation script executes): c:/Python27/python ../feedvalidator/feedvalidator/src/demo.py https://das.dynalias.org:8080/das_core/das/2.16.840.1.113883.4.349/1012581676V377802/otherAdminData/careCoordinators and from java if I leave off the URL parameter and just do: String[] args1 = {“c:/Python27/python”, “../feedvalidator/feedvalidator/src/demo.py” }; Runtime r = Runtime.getRuntime(); Process p = r.exec(args1); it works fine. If I use certain URLs… Read More »

Difference between List and Array [duplicate]

Questions: Possible Duplicate: Primitive Array vs ArrayList What is the difference between List and Array in java? or the difference between Array and Vector! Answers: In general (and in Java) an array is a data structure generally consisting of sequential memory storing a collection of objects. List is an interface in Java, which means that… Read More »

Does the preparedStatement avoid SQL injection? [duplicate]

Questions: This question already has an answer here: How does a PreparedStatement avoid or prevent SQL injection? 9 answers Answers: Using string concatenation for constructing your query from arbitrary input will not make PreparedStatement safe. Take a look at this example: preparedStatement = “SELECT * FROM users WHERE name = ‘” + userName + “‘;”;… Read More »

Keep broadcast receiver running after application is closed

Questions: I need to keep broadcast receiver running all the time after app has been started. Here is the code that registers this receiver in the application IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); BroadcastReceiver mReceiver = new ScreenEventsReceiver(); registerReceiver(mReceiver, filter); And code for receiver public class ScreenEventsReceiver extends BroadcastReceiver { public static boolean wasScreenOn =… Read More »

How to add a UTF-8 BOM in java

Questions: I have a Java stored procedure which fetches record from the table using Resultset object and creates a csv file. BLOB retBLOB = BLOB.createTemporary(conn, true, BLOB.DURATION_SESSION); retBLOB.open(BLOB.MODE_READWRITE); OutputStream bOut = retBLOB.setBinaryStream(0L); ZipOutputStream zipOut = new ZipOutputStream(bOut); PrintStream out = new PrintStream(zipOut,false,”UTF-8″); out.write(‘\ufeff’); out.flush(); zipOut.putNextEntry(new ZipEntry(“filename.csv”)); while (rs.next()){ out.print(“\”” + rs.getString(i) + “\””); out.print(“,”); }… Read More »

Java regular expressions and dollar sign

Questions: I have Java string: String b = “/feedback/com.school.edu.domain.feedback.Review$0/feedbackId”); I also have generated pattern against which I want to match this string: String pattern = “/feedback/com.school.edu.domain.feedback.Review$0(.)*”; When I say b.matches(pattern) it returns false. Now I know dollar sign is part of Java RegEx, but I don’t know how should my pattern look like. I am… Read More »

Randomly select an item from a list

Questions: How can I randomly select an item from a list in java? eg. I have List<String> list = new ArrayList<String>(); list.add(“One”); list.add(“Two”); etc…. How can I randomly select from this list using the Random myRandomizer = new Random(); Answers: Something like this? Random randomizer = new Random(); String random = list.get(randomizer.nextInt(list.size())); Questions: Answers: Clean… Read More »