public class microsoftSQLServer {

public PreparedStatement ps=null;//用于发送SQL语句,增删改

Connection ct=null;//连接

public CallableStatement cstmt = null;

/**

*

* @param sql sql命令或存储过程名称

* @param type 是否为存储过程

*/

public MicrosoftsqlServer(String sql,boolean isProc)

{

try {

//加载驱动

("ok0");

("com.micro;);

Cla("com.micro;);

Sy("ok1");

//得到连接

ct = DriverManager.getConnection(

"jdbc:sqlserver://127.0.0.1:1433;DatabaseName=Test",

"sa", "数据库密码");

("连接数据库成功");

if(isProc){

cstmt=ct.prepareCall(sql);

}else{

ps = ct.prepareStatement(sql);

}

} catch (Exception e) {

// TODO: handle exception

("数据库连接不成功");

}finally{

("操作完成");

}

}

/**

* 执行增删改的命令

* @return 受响应行数

*/

public int executeSql_updata()

{

int result=0;

try {

result = ();

return result;

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

return 0;

}finally{

try {

();

ct.close();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

("连接已关闭");

}

}

/**

* 执行查询

* @return ArrayList<HashMap<String, String>>

*/

public ArrayList<HashMap<String, String>> executeSql_select()

{

ResultSet rs=null;

ArrayList<HashMap<String, String>> list=new ArrayList<HashMap<String,String>>();

try {

rs = ();

ResultSetMetaData data = rs.getMetaData();

while()){

HashMap<String, String> hm=new HashMap<String, String>();

for (int i = 1; i <= da(); i++) {

// 获得指定列的列名

String columnName = da(i);

// 获得指定列的数据类型名

String columnTypeName = da(i);

(columnName+"———–"+columnTypeName);

(columnName, rs.getString(i));

}

li(hm);

}

("查询结束,查询到的list的长的为:========"+li());

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

try {

rs.close();

();

ct.close();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

("连接已关闭");

}

return list;

}

/**

* 执行一个增删改的存储过程命令

* @return 受响应行数

*/

public int callProcUpdate()

{

int result=-1;

try {

result = c();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

try {

c();

ct.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return result;

}

/**

* 执行一个查询的存储过程

* @return ArrayList<HashMap<String, String>>

*/

public ArrayList<HashMap<String, String>> callProcSelect()

{

ResultSet rs=null;

ArrayList<HashMap<String, String>> list=new ArrayList<HashMap<String,String>>();

try {

rs = c();

ResultSetMetaData data = rs.getMetaData();

while()){

HashMap<String, String> hm=new HashMap<String, String>();

for (int i = 1; i <= da(); i++) {

// 获得指定列的列名

String columnName = da(i);

// 获得指定列的数据类型名

String columnTypeName = da(i);

(columnName+"———–"+columnTypeName);

(columnName, rs.getString(i));

}

li(hm);

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

try {

c();

ct.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return list;

}

}

相关推荐