Advertisement
bestTamken

Untitled

Feb 11th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.88 KB | None | 0 0
  1. package com.quanlytoanha.autowritesql;
  2.  
  3.  
  4.  
  5.  
  6.  
  7. import java.sql.*;
  8.  
  9. public class AutoWriteSQL {
  10.  
  11.  
  12.  
  13.     public Connection getConnection() {
  14.         try {
  15.             Class.forName("com.mysql.cj.jdbc.Driver");
  16.             String url = "jdbc:mysql://localhost:3306/quanlytoanha";
  17.             String user = "root";
  18.             String password = "123";
  19.             return DriverManager.getConnection(url, user, password);
  20.         } catch (ClassNotFoundException | SQLException e) {
  21.             return null;
  22.         }
  23.     }
  24.  
  25.    
  26.     public void autoWriteUpdateSQL() {
  27.         Connection connection = getConnection();
  28.         try {
  29.             PreparedStatement statement = connection.prepareStatement("select *from user");
  30.             ResultSet rs = statement.executeQuery();
  31.  
  32.             ResultSetMetaData rsmd = rs.getMetaData();
  33.  
  34.             int count = rsmd.getColumnCount();
  35.             StringBuilder sql = new StringBuilder("update  " + rsmd.getTableName(1) +" set ");
  36.             for (int i = 2 ; i <=count ; i++) {
  37.  
  38.                 if(i==count){
  39.                     sql.append(rsmd.getColumnName(i) + " = ?  " );
  40.                 } else {
  41.                     sql.append(rsmd.getColumnName(i) + " = ? , " );
  42.                 }
  43.  
  44.  
  45.             }
  46.             sql.append(" where id = ? ");
  47.             System.out.println(sql.toString());
  48.  
  49.         }catch (SQLException e){
  50.             System.out.println(e.getMessage());
  51.         }
  52.  
  53.     }
  54.  
  55.     public void autoWriteInsertSQL() {
  56.         Connection connection = getConnection();
  57.         try {
  58.             PreparedStatement statement = connection.prepareStatement("select *from user");
  59.             ResultSet rs = statement.executeQuery();
  60.  
  61.             ResultSetMetaData rsmd = rs.getMetaData();
  62.  
  63.             int count = rsmd.getColumnCount();
  64.             StringBuilder sql = new StringBuilder("insert into  " + rsmd.getTableName(1) +" (  ");
  65.             for (int i = 2 ; i <=count ; i++) {
  66.  
  67.                 if (rsmd.getColumnName(i).equals("modifieddate"))  {
  68.                     continue;
  69.                 }
  70.  
  71.                 if (rsmd.getColumnName(i).equals("modifiedby")) {
  72.                     sql.delete(sql.length()-2 , sql.length());
  73.                     sql.append(" )");
  74.                     break;
  75.                 }
  76.  
  77.                 sql.append(rsmd.getColumnName(i) + " , " );
  78.  
  79.             }
  80.  
  81.             sql.append(" values( ");
  82.  
  83.             for (int i = 2 ; i <=count-2 ; i++) {
  84.  
  85.                 if(i==count-2){
  86.                     sql.append("? ) ");
  87.                 } else {
  88.                     sql.append("?, ");
  89.                 }
  90.  
  91.             }
  92.  
  93.  
  94.             System.out.println(sql.toString());
  95.  
  96.         }catch (SQLException e){
  97.             System.out.println(e.getMessage());
  98.         }
  99.     }
  100.  
  101.     public static void main(String[] args) {
  102.         autoWriteInsertSQL();
  103.         autoWriteUpdateSQL()
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement