JDBC:灵活指定SQL中的变量(SXT)

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

 JDBC:灵活指定SQL中的变量(SXT)

 

Demo

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