zoukankan      html  css  js  c++  java
  • javaweb入门(使用SQLserver2008 R2数据库)

    一.数据库的使用

    1 加载驱动

    1 try {
    2             
    3         Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
    4             
    5     } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
    6             // TODO Auto-generated catch block
    7             e.printStackTrace();
    8     }

    2.创建链接对象

     1 String user = "sa";
     2 String password = "jisuan@10Q";
     3 String url = "jdbc:sqlserver://localhost:1433;DatabaseName=jaovo_msg";
     4 Connection connection = null;
     5 try {
     6      connection = DriverManager.getConnection(url,user,password);
     7             
     8     } catch (SQLException e) {
     9             
    10       e.printStackTrace();
    11     }


    3 创建语句传输对象

    1 String sql = "select count(*) from t_login where username=? And password = ?";
    2 PreparedStatement preparedStatement = null;3 4 preparedStatement = connection.prepareStatement(sql);

    4 接收结果集对象

    1 ResultSet resultSet = null;
    2 
    3 resultSet = preparedStatement.executeQuery();


    5 遍历

    while(resultSet.next()) 
    {
        if (resultSet.getInt(1) > 0) 
        {
               system.ou.println("就是这个!");
               return;
        }
    }
                            


    6 关闭资源

    1 DBUtil.close(resultSet);
    2 DBUtil.close(preparedStatement);
    3 DBUtil.close(connection);
  • 相关阅读:
    java中栈内存和堆内存的简单理解
    java中构造函数与一般函数的区别

    另一部漫画
    海边的卡夫卡
    11-12
    这篇大概值一百万吧
    我的千岁寒
    11-9
    嗯……………股票已经涨的我不想上班了
  • 原文地址:https://www.cnblogs.com/tianxiayoujiu/p/7881218.html
Copyright © 2011-2022 走看看