/*
* 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;
}
}
java原生数据库工具类
最新推荐文章于 2024-05-26 16:42:49 发布