Skip to content

Instantly share code, notes, and snippets.

@pikanji
Created January 18, 2013 07:12
Show Gist options
  • Save pikanji/4562891 to your computer and use it in GitHub Desktop.
Save pikanji/4562891 to your computer and use it in GitHub Desktop.
Sample to access MySQL from Spring MVC.
try {
Class.forName("com.mysql.jdbc.Driver"); // Load driver
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost/db_name?useUnicode=true&characterEncoding=UTF-8",
"db_user",
"db_password"
);
Statement state = con.createStatement();
String sql = "INSERT INTO `users` (`username`, `password`) VALUES ('hoge', 'fuga')";
state.execute(sql);
con.close();
} catch (ClassNotFoundException e) {
logger.error(e.getMessage());
} catch (SQLException e) {
logger.error(e.getMessage());
} catch (Exception e) {
logger.error(e.getMessage());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment