连接数据库的工具类
2018-02-28 11:54
239 查看
public class JdbcUtils {
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/test1";
private static String user = "root";
private static String password = "123456";
// 获取连接对象
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
// 执行增删改的sql语句的方法
public static int exec(String sql) {
Connection conn = getConnection();
int num = 0;
try {
Statement stmt = conn.createStatement();
num = stmt.executeUpdate(sql);
close(conn,stmt,null);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return num;
}
// 关闭资源
public static void close(Connection conn, Statement stmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
登录的案例
public class LoginDemo01 {
public static void main(String[] args) throws Exception {
//获取用户输入的用户名和密码
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = scanner.nextLine();
System.out.println("请输入密码:");
String password = scanner.nextLine();
// System.out.println(username+" "+password);
//1.
Connection conn = JdbcUtils.getConnection();
//2.
Statement stmt = conn.createStatement();
//3.
String sql="SELECT * FROM t_user WHERE username=? AND PASSWORD=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
//第一个参数:?的索引值,从1开始
//第二个参数:?处对应的值
pstmt.setString(1, username);
pstmt.setString(2, password);
ResultSet rs = pstmt.executeQuery();
User user=null;
if(rs.next()){
user= new User(rs.getString(1),rs.getString(2),rs.getString(3));
}
//4.关闭资源
//PreparedStatement是Statement的子接口
JdbcUtils.close(conn, pstmt, rs);
//判断用户是否登录成功
if(user!=null){
System.out.println("登录成功!");
}else{
System.out.println("登录失败!");
}
}
}
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/test1";
private static String user = "root";
private static String password = "123456";
// 获取连接对象
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
// 执行增删改的sql语句的方法
public static int exec(String sql) {
Connection conn = getConnection();
int num = 0;
try {
Statement stmt = conn.createStatement();
num = stmt.executeUpdate(sql);
close(conn,stmt,null);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return num;
}
// 关闭资源
public static void close(Connection conn, Statement stmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
登录的案例
public class LoginDemo01 {
public static void main(String[] args) throws Exception {
//获取用户输入的用户名和密码
Scanner scanner = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = scanner.nextLine();
System.out.println("请输入密码:");
String password = scanner.nextLine();
// System.out.println(username+" "+password);
//1.
Connection conn = JdbcUtils.getConnection();
//2.
Statement stmt = conn.createStatement();
//3.
String sql="SELECT * FROM t_user WHERE username=? AND PASSWORD=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
//第一个参数:?的索引值,从1开始
//第二个参数:?处对应的值
pstmt.setString(1, username);
pstmt.setString(2, password);
ResultSet rs = pstmt.executeQuery();
User user=null;
if(rs.next()){
user= new User(rs.getString(1),rs.getString(2),rs.getString(3));
}
//4.关闭资源
//PreparedStatement是Statement的子接口
JdbcUtils.close(conn, pstmt, rs);
//判断用户是否登录成功
if(user!=null){
System.out.println("登录成功!");
}else{
System.out.println("登录失败!");
}
}
}
相关文章推荐
- java中常用的工具类(三)连接数据库的综合类,DES加密和解密,Base64,ExcelUtil工具类,
- 自定义数据库连接工具类
- JDBC【介绍JDBC、使用JDBC连接数据库、简单的工具类】
- 数据库连接工具类
- 编写连接数据库的工具类
- JDBC第一篇--【介绍JDBC、使用JDBC连接数据库、简单的工具类】
- JDBC连接数据库工具类及连接Access数据库示例
- java(2014版)连接数据库的工具类
- java 数据库连接工具类
- JDBC概念原理和连接数据库以及工具类的书写
- 使用JDBC链接数据库之连接数据库通用工具类
- 连接数据库的工具类,实现数据的插入
- 连接数据库的工具类,以及相关用法
- MyBatisUtil.java.工具类,连接数据库库
- 使用配置文件和工具类连接数据库
- 建立mysql与数据库连接和释放资源 工具类
- 自己写一个数据库连接工具类
- 连接数据库工具类调用程序
- java连接数据库工具类—JDBC
- 连接数据库Jdbc工具类