Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. package main;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.sql.Timestamp;
  9. import java.util.Date;
  10. import java.util.Properties;
  11.  
  12. public class Main2 {
  13. public static void main(String[] args) {
  14. String USER="";
  15. String PASS="";
  16. Connection conn = null;
  17. Statement stmt=null;
  18. PreparedStatement pstmt=null;
  19. ResultSet rs=null;
  20. try {
  21. Class.forName("org.postgresql.Driver");
  22. String url = "jdbc:postgresql://localhost/db_test01";
  23. Properties props = new Properties();
  24. props.setProperty("user",USER);
  25. props.setProperty("password",PASS);
  26. conn = DriverManager.getConnection(url, props);
  27. System.out.println("接続に成功しました。");
  28.  
  29. //pstmt=conn.prepareStatement("INSERT INTO public.\"PRODUCT\"(\"PRODUCT_CODE\",\"PRODUCT_NAME\",\"PRICE\",\"UPDATE_DATE\") VALUES(?,?,?,?)");
  30. pstmt=conn.prepareStatement("insert into public.product(product_code,product_name,price,update_date) values(?,?,?,?)");
  31. pstmt.setString(1, "BIKE002");
  32. pstmt.setString(2, "cannondale slate");
  33. pstmt.setInt(3, 200000);
  34. pstmt.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
  35.  
  36. int count=pstmt.executeUpdate();
  37. System.out.println(count+"件のinsertに成功しました。");
  38. pstmt.close();
  39.  
  40. stmt=conn.createStatement();
  41. String sql="select * from product";
  42. rs=stmt.executeQuery(sql);
  43.  
  44. while(rs.next()){
  45. String productCode=rs.getString("product_code");
  46. String productName=rs.getString("product_name");
  47. int price=rs.getInt("price");
  48. Date updateDate=rs.getTimestamp("update_date");
  49. System.out.println(rs.getRow()+"行目のデータ");
  50. System.out.println(productCode);
  51. System.out.println(productName);
  52. System.out.println(price);
  53. System.out.println(updateDate);
  54. }
  55. rs.close();
  56. stmt.close();
  57. } catch (ClassNotFoundException e) {
  58. e.printStackTrace();
  59. } catch (SQLException e) {
  60. System.out.println(e.getErrorCode());
  61. System.out.println(e.getSQLState());
  62. System.out.println(e.getMessage());
  63. } finally {
  64. try{
  65. if(stmt!=null){
  66. stmt.close();
  67. }
  68. }catch (SQLException e) {
  69. e.printStackTrace();
  70. }
  71.  
  72. try{
  73. if(pstmt!=null){
  74. pstmt.close();
  75. }
  76. }catch (SQLException e) {
  77. e.printStackTrace();
  78. }
  79.  
  80. try {
  81. if (conn != null) {
  82. conn.close();
  83. System.out.println("切断しました。");
  84. }
  85. } catch (SQLException e) {
  86. System.out.println(e.getErrorCode());
  87. System.out.println(e.getSQLState());
  88. System.out.println(e.getMessage());
  89. }
  90. }
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement