package bao;
import java.sql.*;
import java.util.*;
public class DAOoperation {
Statement st;
Connection cn;
ResultSet rs;
ResultSetMetaData rsmd;
int num,i;
public DAOoperation(String serverName,String dataBase,String userName,String passWord) {
this.connect(serverName,dataBase,userName,passWord);
}
public void connect(String serverName,String dataBase,String userName,String passWord){
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
cn=DriverManager.getConnection("jdbc:microsoft:sqlserver://"+serverName+":1433;databasename="+dataBase,userName,passWord);
} catch (SQLException e) {
e.printStackTrace();
}
}
public Vector getData(){
Vector data=new Vector();
try {
st=cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
} catch (SQLException e) {
e.printStackTrace();
}
try {
rs=st.executeQuery("select name,number from roots");
} catch (SQLException e) {
e.printStackTrace();
}
try {
rsmd=rs.getMetaData();
} catch (SQLException e) {
e.printStackTrace();
}
try {
num=rsmd.getColumnCount();
} catch (SQLException e1) {
e1.printStackTrace();
}
Vector row;
try {
while(rs.next()){
row=new Vector();
for(i=1;i<num+1;i++){
String s=rs.getString(i);
row.add(s);
}
data.add(row);
}
} catch (SQLException e) {
e.printStackTrace();
}
return data;
}
public boolean addData(String name,String number){
boolean flag=false;
try {
st.executeUpdate("insert into roots values('"+name+"','"+number+"')");
} catch (SQLException e) {
e.printStackTrace();
}
flag=true;
return flag;
}
}
评论1