java连接sql2005数据库类
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JOptionPane;
public class JADO {
//private final String DRIVER = "com.mysql.jdbc.Driver";
private final String DRIVER ="com.microsoft.sqlserver.jdbc.SQLServerDriver";
//Connection con = null;
//Statement sql = null;
private Connection conn = null;
private PreparedStatement s_query;
private PreparedStatement s_update;
private PreparedStatement s_insert;
private ResultSet re = null;
public JADO() throws SQLException{//,String user , String pwd
try {
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=EmployeeSalaryManage";
String userName = "sa"; // 默认用户名
String userPwd = "123456"; // 密码
Class.forName(DRIVER);
conn = DriverManager.getConnection(dbURL, userName, userPwd);
// JOptionPane.showMessageDialog(null, "连接成功.");
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
System.out.println("connection false");
}
}
public ResultSet Query(String sql){
re = null;
try {
// s_query=conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
s_query=conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
re =s_query.executeQuery();
return re;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Statement is false");
}
return re;
}
public ResultSet Query(String sql,Object[] title){
re = null;
try {
s_query=conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
for(int i=0;i<title.length;i++){
s_query.setObject(i+1, title[i]);
}
re =s_query.executeQuery();
return re;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Statement is false");
}
return re;
}
public ResultSet Query(String sql,Vector title){
re = null;
try {
s_query=conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
for(int i=0;i<title.size();i++){
s_query.setObject(i+1, title.get(i));
}
re =s_query.executeQuery();
return re;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Statement is false");
}
return re;
}
public int Insert(String sql){
try {
s_insert = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
return s_insert.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
public int Insert(String sql,Object []insert){
try {
s_insert = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
for(int i =0;i<insert.length;i++){
s_insert.setObject(i+1, insert[i]);
}
return s_insert.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
public int Insert(String sql,Vector insert){
try {
s_insert = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
for(int i =0;i<insert.size();i++){
s_insert.setObject(i+1, insert.get(i));
}
return s_insert.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
public int Update(String sql){
try {
s_update = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
return s_update.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
public int Update(String sql,Object [] update){
try {
s_update = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
for(int i =0;i<update.length;i++){
s_query.setObject(i+1, update[i]);
}
return s_update.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
public int Update(String sql,Vector update){
try {
s_update = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
for(int i =0;i<update.size();i++){
s_query.setObject(i+1, update.get(i));
}
return s_update.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
public int Count(/*String sql*/) throws SQLException{
// int count=0;
// ResultSet re =Query(sql);
re.last();
return re.getRow();
//while(re.next()){
// count++;
//}
//re.
//return count;
}
public void close(){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param args
* @throws SQLException
* @throws UnsupportedEncodingException
*/
/*public static void main(String[] args) throws SQLException, UnsupportedEncodingException {
}*/
}
可使用的类,封装完成,只适用于sqlserver数据库,用于mysql需要修改关键字,这里不多讲了!