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

 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. }

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值