1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.List; import edu.sjsu.cs.blogs.Blog; public class MySQLDB { private Connection connection = null; private String driverName = "org.gjt.mm.mysql.Driver"; private String serverName = "localhost"; private String mydatabase = "testdb"; private String url = ""; private String username = "root"; private String password = "root"; private String sql = "INSERT INTO BLOG VALUES(?, ?, ?, ? , ?, ?, ?, ?, ?)"; PreparedStatement pstmt = null; public MySQLDB() { try { this.url = "jdbc:mysql://" + serverName + "/" + mydatabase; Class.forName(driverName); connection = DriverManager.getConnection(url, username, password); pstmt = connection.prepareStatement(sql); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } public void writeBlogsToDB(List<Blog> blogs) { for(Blog blog: blogs) { try { pstmt.setString(1, blog.getTitle()); pstmt.setString(2, blog.getUrl()); pstmt.setString(3, blog.getSummary()); pstmt.setInt(4, blog.getNumOfPosts()); pstmt.setInt(5, blog.getNumOfComments()); pstmt.setString(6, blog.getBlogText()); pstmt.setString(7, blog.getBlogRollLinks()); pstmt.setString(8, blog.getCitationLinks()); pstmt.setInt(9, blog.getBlogAge()); pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } } public void retrieveBlogsFromDB() { } public Connection getConnectionInstance() { return this.connection; } }
Refactorings
No refactoring yet !
Maros
May 31, 2008, May 31, 2008 12:11, permalink
I don't really know if this is better or not, but it's an alternative way to do 'writeBlogsToDB', so refactoring it is :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
... public void writeBlogsToDB(List<Blog> blogs) { for(Blog blog: blogs) { try { Object[] parameters = new Object[] { blog.getTitle(), blog.getUrl(), blog.getSummary(), Integer.valueOf(blog.getNumOfPosts()), Integer.valueOf(blog.getNumOfComments()), blog.getBlogText(), blog.getBlogRollLinks(), blog.getCitationLinks(), Integer.valueOf(blog.getBlogAge()) }; for (int index = 0; index < parameters.length; ++i) pstmt.setObject(index, parameters[index]); pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } } ...
Maros
May 31, 2008, May 31, 2008 12:34, permalink
This way it is easier to modify the parameter list. Even the 'sql' field (number of question marks) can be computed from PARAMETERS. The price is worse readability, worse compiler checks and worse performance :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
... private static final String[] PARAMETERS = new String[] { "Title", "Url", "Summary", "NumOfPosts", "NumOfComments", "BlogText", "BlogRollLinks", "CitationLinks", "BlogAge" }; public void writeBlogsToDB(List<Blog> blogs) { for (Blog blog : blogs) { try { for (int index = 0; index < PARAMETERS.length; ++index) pstmt.setObject(index, Blog.class.getMethod( "get" + PARAMETERS[index]).invoke(blog)); pstmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } } } ...
I'm using this as the core class to read from and write stuff to the database.