一.数据库的使用

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);

 

相关文章:

  • 2022-12-23
  • 2021-12-06
  • 2021-08-01
  • 2022-12-23
  • 2021-11-20
  • 2022-12-23
  • 2021-09-17
猜你喜欢
  • 2022-12-23
  • 2021-04-11
  • 2022-12-23
  • 2021-04-03
  • 2021-09-30
  • 2021-11-29
  • 2021-08-06
相关资源
相似解决方案