一.数据库的使用
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);