在写完JdbcUtils工具类之后,就可以测试调用一下。
写一个登录测试。并且通过传入账号密码,返回一个boolen的类型,判断是否登录成功。
package cn.tong.login;import java.sql.ResultSet;import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;import cn.tong.jdbc.utils.JdbcUtils;public class UserLogin {public boolean UserLogin(String username, String password) throws Exception{// 获取链接对象Connection connection = JdbcUtils.getConnection();Statement statement = (Statement) connection.createStatement();// 写sql语句// 插入String sql = "insert into user(username, password) values ('"+username+"', "+password+")";// 查询String sql = "select * from user where username = '"+username+"'and password = '"+password+"'";ResultSet resultSet = statement.executeQuery(sql);JdbcUtils.close(resultSet, statement, connection);return resultSet.next();}
}
上面是一个传入参数并且返回boolen类型的类。
下面写一个调用方法的测试类
package cn.tong.login;public class UserLoginTest {public static void main(String[] args) throws Exception {String username = "zhangsan";String password = "1233";UserLogin login = new UserLogin();boolean result = login.UserLogin(username, password);System.out.println(result);}}
这样通过运行UserLoginTest就可以通过返回的woolen值去判断是否成功。