For a database project (building a full retail store system) I've tried passing a java.util.Date to a Java prepared statement. The problem is that the prepared statement expects java.sql.date, and converting it isn't working for me. Here's the errant code: ps.setDate(2, emp.getDate()); which results in this error: no suitable constructor found for Date(java.util.Date) constructor java.sql.Date.Date(long) is not applicable (actual argument java.util.Date cannot be converted to long by method invocation conversion) constructor java.sql.Date.Date(int,int,int) is not applicable (actual and formal argument lists differ in length) The fix is to create an SQL Date using the java.util.Date as follows: java.sql.Date sqlDate = new java.sql.Date(emp.getDate().getTime()); Then, the prepared statement accepts the date. ps.setDate(2, sqlDate);
So excited I can't sleep
Seriously, I can't sleep. I'm sitting here in Eclipse messing with these layouts and getting stoked for the BYU Mobile App Competition. A partner and I are developing a super awesome Android app that is going to be a blast. Too bad I can't say what it is yet. But, in about three weeks we should be ready to push it out. We're under way right now and will be working on it as much as possible to get it ready for the competition. Man, I feel like saying more but I guess I won't for now. People could be out there snatching up our idea as we speak. That sucks. But believe me once it's out there I'll be all about spreading the news. The cool thing is that this app has never been done before on Android, it's a fresh market! There's one on the iOS app store, but it's lame. Maybe we'll try xcode and iphone development if this goes well.
Here's how the BYU mobile app competition works:
- Teams of 1 to 4, with at least one member a current BYU student
- Build your app for Android, iOS, WP7, or Blackberry
- Release it to the Market/App Store November 2-16
- Cash prizes as follows:
- Grand Prize: $6,000
- Analytics 1st Place: $3,000
- Judged 1st Place: $3,000
- Analytics 2nd Place: $1,500
- Judged 2nd Place: $1,500
- Audience Choice: All team members will be awarded with an iPad 2 (or 3). This award will be based on audience input at the final event
Building a Web Scraper
ISYS 403. Business oriented programming. Goal: Program a news aggregator. Project statement: Program a news aggregator that scrapes a news page and posts the top news stories. The assignment gives a view into how search engines and other scrapes work online.
Many sources publish the news: local radio and TV stations, the Associated Press, national and international sources, and aggregators. Aggregators like Google News don’t actually create news stories; instead, they parse the news stories created by other sources, discover like stories, and publish a combined view.
The problem is each site on the web publishes in HTML — a plain-text, free-flowing format. You’ll have to code a technique called page scraping. Page scraping is programatically going through a retrieved HTML source file and picking specific data pieces, such as news headlines and links, from the HTML.
Regular expressions are one of the best text parsing techniques available. Beyond parsing HTML, they are useful for searching through all types of free-form text.
Completed project using businessweek.com/technology:
I thought this project was awesome! I've been wanting to see how Java interfaces with the web and this was it. My first time using java to hit the web and it really wasn't too bad. Downloading the URL's HTML to a string was surprisingly simple. My code looked something like this:
String lineOfHTML = ""; String content = ""; //download home page try { URL url = new URL("http://www.businessweek.com/technology/"); BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); while ((lineOfHTML = reader.readLine()) != null) { content += lineOfHTML; } reader.close(); } catch (IOException e) { e.printStackTrace(); }
As you can see the hard part really isn't that hard. I used the URL library to grab the webpage and a buffered reader to read in all the HTML, then just stored it as a string. The samples out there on bing made it really easy to. Try searching "download webpage in java" on bing. You'll get a lot of hit, trust me.
Parsing the HTML to find the new stories meant looking up businessweek's HTML source code, finding the tags surrounding news stories, and using regex to grab the information we wanted, which was the name, link, and description in our case. My regex expression ended up looking like this in order to get the top two stories: String regex = "
(.*?)
(.*?)
"; Fun eh?So that was it. Put it together and you have the news parser as pictured above!