JDBC处理DML(SXT)

来源:互联网 发布:vb控制鼠标点击 源码 编辑:程序博客网 时间:2024/06/05 15:35

 JDBC处理DML(SXT)

 

Demo1

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import com.sun.org.apache.xalan.internal.xsltc.runtime.Parameter;
  7. public class TestDML
  8. {
  9.     /**
  10.      * @JDBC处理DML。接收手动输入的参数
  11.      */
  12.     public static void main(String[] args)
  13.     {
  14.         // TODO Auto-generated method stub
  15.         if (args.length != 4)
  16.         {
  17.             System.out.println("Error,plesase input again!");
  18.             System.exit(-1);
  19.         }
  20.         int userid = 0;
  21.         try
  22.         {
  23.             userid = Integer.parseInt(args[0]);
  24.         } catch (NumberFormatException e)
  25.         {
  26.             System.out.println("Number error");
  27.             System.exit(-1);
  28.         }
  29.         String usernameString = args[1];
  30.         String passwordString  = args[2];
  31.         String userflaString = args[3];
  32.         Connection conn = null;
  33.         Statement stmt = null;
  34.         ResultSet rs = null;
  35.         try
  36.         {
  37.             Class.forName("com.mysql.jdbc.Driver");
  38.             String url = "jdbc:mysql://localhost:3306/college";
  39.             String user = "root";
  40.             String password = "123456";
  41.             conn = DriverManager.getConnection(url, user, password);
  42.             stmt = conn.createStatement();
  43.             String sql = "insert into user (userId,userName,userPassword,userFlag)values(" + userid+",'"+usernameString+"','"+passwordString+"','" +userflaString+ "')";
  44.             stmt.executeUpdate(sql);
  45.             System.out.println(sql);
  46.             rs = stmt.executeQuery("select * from user where userName='testDML4'");
  47.             while (rs.next())
  48.             {
  49.                 System.out.print("username:");
  50.                 System.out.println(rs.getString("userName"));
  51.                 System.out.print("password:");
  52.                 System.out.println(rs.getString("userPassword"));
  53.             }
  54.         } catch (ClassNotFoundException e)
  55.         {
  56.             // TODO Auto-generated catch block
  57.             e.printStackTrace();
  58.         } catch (SQLException e)
  59.         {
  60.             // TODO Auto-generated catch block
  61.             e.printStackTrace();
  62.         } finally
  63.         {
  64.             try
  65.             {
  66.                 if (rs != null)
  67.                 {
  68.                     rs.close();
  69.                     rs = null;
  70.                 }
  71.                 if (stmt != null)
  72.                 {
  73.                     stmt.close();
  74.                     stmt.close();
  75.                 }
  76.                 if (conn != null)
  77.                 {
  78.                     conn.close();
  79.                     conn = null;
  80.                 }
  81.             } catch (SQLException e)
  82.             {
  83.                 // TODO Auto-generated catch block
  84.                 e.printStackTrace();
  85.             }
  86.         }
  87.     }
  88. }

Demo2

  1. import java.sql.*;
  2. /**
  3.  * JDBC处理DML2。无输入参数。
  4.  */
  5. public class TestJDBCDML
  6. {
  7.     /**
  8.      * @param args
  9.      */
  10.     public static void main(String[] args)
  11.     {
  12.         Connection conn = null;
  13.         Statement stmt = null;
  14.         ResultSet rs = null;
  15.         try
  16.         {
  17.             Class.forName("com.mysql.jdbc.Driver");
  18.             
  19.             String url = "jdbc:mysql://localhost:3306/college";
  20.             String user = "root";
  21.             String password = "123456";
  22.             conn = DriverManager.getConnection(url, user, password);
  23.             
  24.             stmt = conn.createStatement();
  25.             
  26.             String sql = "insert into user (userName,userPassword,userFlag)values('testDML2','123','dml')";
  27.             stmt.executeUpdate(sql);
  28.             
  29.             rs = stmt.executeQuery("select * from user where userName='testDML2'");
  30.             
  31.             while(rs.next())
  32.             {
  33.                 System.out.print("username:");
  34.                 System.out.println(rs.getString("userName"));
  35.                 System.out.print("password:");
  36.                 System.out.println(rs.getString("userPassword"));
  37.                 
  38.             }
  39.         } catch (ClassNotFoundException e)
  40.         {
  41.             // TODO Auto-generated catch block
  42.             e.printStackTrace();
  43.         } catch (SQLException e)
  44.         {
  45.             // TODO Auto-generated catch block
  46.             e.printStackTrace();
  47.         } finally
  48.         {
  49.             try
  50.             {
  51.                 if(rs != null)
  52.                 {
  53.                     rs.close();
  54.                     rs = null;
  55.                 }
  56.                 
  57.                 if(stmt != null)
  58.                 {
  59.                     stmt.close();
  60.                     stmt.close();
  61.                 }
  62.                 
  63.                 if (conn != null)
  64.                 {
  65.                     conn.close();
  66.                     conn = null;
  67.                 }
  68.                 
  69.             } catch (SQLException e)
  70.             {
  71.                 // TODO Auto-generated catch block
  72.                 e.printStackTrace();
  73.             }
  74.         }
  75.     }
  76. }

 

原创粉丝点击