zoukankan      html  css  js  c++  java
  • JDBC工具包

    package com.neuedu.servlet;


    import java.awt.List;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;


    public class Jdbcutil {
    //JDBC的连接方法
    public static Connection getConnection() {
    Connection con=null;
    try {
    //加载驱动
    Class.forName("com.mysql.jdbc.Driver");
    //创建连接
    con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");

    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return con;
    }
    //JDBC的关闭方法
    public static void close(Connection con){

    try {
    if(con!=null){
    con.close();//关闭连接
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }
    //插入方法
    public static int insert(String sql,Object[] params){
    int result=0;

    Connection con=getConnection();
    try {
    PreparedStatement pstmt=con.prepareStatement(sql);
    if(params!=null){
    for(int i=0;i<params.length;i++){
    pstmt.setObject(i+1, params[i]);
    }
    result=pstmt.executeUpdate();
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    if(con!=null){
    close(con);
    }
    }

    return result;
      }
    //更新方法
    public static int update(String sql,Object[] params){
    int result=0;
    Connection con=getConnection();
    try {
    PreparedStatement pstmt=con.prepareStatement(sql);
    if(params!=null){
    for(int i=0;i<params.length;i++){
    pstmt.setObject(i+1, params[i]);
    }
    result=pstmt.executeUpdate();
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    close(con);
    }

    return result;
    }
    //删除方法
    public static int delete(String sql,Object[] params){
    Connection con=Jdbcutil.getConnection();
    int a=0;
    try {
    PreparedStatement pstms=con.prepareStatement(sql);
    if(params!=null){
    for(int i=0;i<params.length;i++){
    pstms.setObject(i+1, params[i]);
    }
    a=pstms.executeUpdate();
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally {


    if(con!=null){
    close(con);
    }
    }
    return a;
    }
    //通过ID查找用户是否存在
    public static int findById(String sql,int id){
    int result=0;
    Connection conn=getConnection();
    try {
    PreparedStatement prepareStatement = conn.prepareStatement(sql);
    prepareStatement.setInt(1, id);
    ResultSet rs = prepareStatement.executeQuery();
    if(rs.next()){
    result=1;
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return result;
    }
    //通过姓名查找用户是否存在
    public static int findByName(String sql,String username){
    int result=0;
    Connection conn=getConnection();
    try {
    PreparedStatement prepareStatement = conn.prepareStatement(sql);
    prepareStatement.setString(1, username);
    ResultSet rs = prepareStatement.executeQuery();
    if(rs.next()){
    result=1;
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return result;
    }
    //通过姓名密码找是否用户存在
    public static int findByNameAndPassword(String sql,String username,String password){
    int result=0;
    Connection conn=getConnection();
    try {
    PreparedStatement prepareStatement = conn.prepareStatement(sql);
    prepareStatement.setString(1, username);
    prepareStatement.setString(2, password);
    ResultSet rs = prepareStatement.executeQuery();
    if(rs.next()){
    result=1;
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return result;
    }
    }
  • 相关阅读:
    构建之法阅读笔记03
    12.16第三周总结
    构建之法阅读笔记02
    12.9第二周周总结
    四则运算2
    构建之法阅读笔记01
    12.2第一周总结
    课堂练习-增加信息
    软件工程00
    web自动化测试---web页面元素的定位
  • 原文地址:https://www.cnblogs.com/mazhitao/p/7424426.html
Copyright © 2011-2022 走看看