1. 程式人生 > >JDBCstatement批量新增資料

JDBCstatement批量新增資料

public boolean insert(Map<String, String> map){

         boolean i = false;

         try {

              Class.forName("com.mysql.jdbc.Driver");

              Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db214", "root", "root");

              Statement stmt = conn.createStatement();

              String empno = map.get("empno");

              String ename = map.get("ename");

              String job = map.get("job");

              String sal = map.get("sal");

              String deptno = map.get("deptno");

              String sql="insert into t48_emp(empno,ename,job,sal,deptno) values("+empno+",'"+ename+"','"+job+"',"+sal+","+deptno+");";

              i = stmt.execute(sql);

              System.out.println(i);

              if(i){

                  System.out.println("資料插入成功");

              }else {

                  System.out.println("資料插入失敗!");

              }

              return i;

         } catch (ClassNotFoundException | SQLException e) {

              // TODO Auto-generated catch block

              e.printStackTrace();

              return i;

         }

     }

public static void main(String[] args) {

         JdbcDao2 jd = new JdbcDao2();

         Map<String, String> map = new HashMap();

         map.put("empno", "10005");

         map.put("ename", "nihao");

         map.put("job", "clerk");

         map.put("sal", "1000");

         map.put("deptno", "20");

         jd.insert(map);

     }