java原生数据库工具类

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package job.tot.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
 *
 * @author totcm
 */
public class TcydUtil {
  static String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
  static String dburl = "jdbc:sqlserver://localhost:1433;DatabaseName = aaa;characterEncoding=utf-8";
  static String userName = "dbyd";//填写自己的数据库名称
  static String userPwd = "dbyd";//数据库对应密码
  public static  int getN(){
    try {
        Class.forName(driverName);
        System.out.println("连接成功!!!");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        System.out.println("驱动加载失败");
    }
    int ret=0;
    Connection dbcon =null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        dbcon = DriverManager.getConnection(dburl,userName,userPwd);
        ps = dbcon.prepareStatement("SELECT COUNT(*) FROM 车位地址表 where 车位序号 not in(2,3,18,39,40,41) and 占用状态=0");
        rs = ps.executeQuery();
        if(rs.next()){
          ret=rs.getInt(1);
        }
    } catch (SQLException e) {
        e.printStackTrace();
        System.out.println("连接失败");
    }finally{
      try {
        if (rs != null) {
          rs.close();
        }
        if (ps != null){
          ps.clearParameters();
          ps.close();
        }
        if(dbcon!=null){
          dbcon.close();
        }
      } catch (SQLException e) {
      }
    }
    return ret;
  }
  public static boolean add(String carId,long endDt,int IsTrans){
    try {
      Class.forName(driverName);
      System.out.println("连接成功!!!");
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
      System.out.println("驱动加载失败");
    }
    int ret=0;
    Connection dbcon =null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    boolean returnValue=true;        
    try{           
      dbcon = DriverManager.getConnection(dburl,userName,userPwd);
      ps=dbcon.prepareStatement("insert into 待入场车辆(carId,endDt,IsTrans) values(?,?,?)");
      ps.setString(1,carId);            
      ps.setLong(2,endDt);
      ps.setInt(3,IsTrans);
      if(ps.executeUpdate()!=1) returnValue=false;
    } catch(SQLException e){            
    } finally{
      try {
        if (rs != null) {
          rs.close();
        }
        if (ps != null){
          ps.clearParameters();
          ps.close();
        }
        if(dbcon!=null){
          dbcon.close();
        }
      } catch (SQLException e) {
      }
    }
    return returnValue;
  }
}
 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值