package service.db;
import java.sql.*;
import java.util.*;
import com.microsoft.jdbcx.sqlserver.SQLServerDataSource;
public class DBConnect {
//file://You Host IP
private String strHostAddress="192.168.19.3";
//file://Host Port
private int intHostPort=1433;
//file://UserName
private String strUserName="huang";
//file://PassWord
private String strPassWord="huang";
//file://DataBase Name
private String strDataName="HEWSDADA";
//file://Max Connection
//private int intMaxConnection=10;
private Connection con=null;
private Statement stmt=null;
private ResultSet rs=null;
//file://JDBC source
private SQLServerDataSource source=null;
ArrayList ArrayRs=new ArrayList();
/**
* @param 构造函数注册JDBC驱动程序
* */
public DBConnect(){
try{
if(source==null){
source=new SQLServerDataSource();
source.setDatabaseName(strDataName);
source.setServerName(strHostAddress);
source.setPortNumber(intHostPort);
source.setUser(strUserName);
source.setPassword(strPassWord);
//file://source.setHostProcess(intMaxConnection);
}
}catch(Exception e){
System.out.println("open database error:"+e.getMessage());
}
}
/**
* @param executeQuery查询数据库方法
* @param 每条ArrayList记录存为String[] 数组
* @return ArrayList
* @exception SQLException
*/
public ArrayList executeQuery(String strSql) throws SQLException {
rs=null;
try{
con=source.getConnection();
stmt=con.createStatement();
rs=stmt.executeQuery(strSql);
ResultSetMetaData rsmd=rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
file://判断是否为空
if(!ArrayRs.isEmpty()){
ArrayRs.clear();
}
/*
* 将每条记录写入数组
* 将数组放在ArrayList里
*/
while(rs.next()){
String[] strArrayTemp=new String[numberOfColumns];
for(int i=0;i<numberOfColumns;i++){
if(rs.getObject(i+1)==null){
strArrayTemp[i]= "";
}else{
strArrayTemp[i]=rs.getObject(i+1).toString();
}
}
ArrayRs.add(strArrayTemp);
}
return (ArrayList)ArrayRs.clone();
}catch(Exception e){
System.out.println("query error:" + e.getMessage());
}finally{
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
}
return ArrayRs;
}
}这个是我使用的的错误
DBConnect con=new DBConnect();
String sql="select * from user";
try {
System.out.print( con.executeQuery(sql));
System.out.print( con.executeQuery(sql).get(0));
} catch (SQLException e) {
e.printStackTrace();
}
严重: Servlet.service() for servlet action threw exception
java.lang.NoClassDefFoundError: com/merant/jdbcspy/SpyLogger
at com.microsoft.jdbcx.base.BaseDataSource.getConnection(Unknown Source) 这里要加 get和set的方法么?!
at service.db.DBConnect.executeQuery(DBConnect.java:54)
at service.form.LoginForm.validate(LoginForm.java:51)
还有就是ArrayList executeQuery返回ArrayRs这个。
例如数据库里面有两个项,name,password。我怎么使用ArrayRs区分他们呢?!
晚一点我贴出添加,修改,删除的代码的。如果大家需要的话
import java.sql.*;
import java.util.*;
import com.microsoft.jdbcx.sqlserver.SQLServerDataSource;
public class DBConnect {
//file://You Host IP
private String strHostAddress="192.168.19.3";
//file://Host Port
private int intHostPort=1433;
//file://UserName
private String strUserName="huang";
//file://PassWord
private String strPassWord="huang";
//file://DataBase Name
private String strDataName="HEWSDADA";
//file://Max Connection
//private int intMaxConnection=10;
private Connection con=null;
private Statement stmt=null;
private ResultSet rs=null;
//file://JDBC source
private SQLServerDataSource source=null;
ArrayList ArrayRs=new ArrayList();
/**
* @param 构造函数注册JDBC驱动程序
* */
public DBConnect(){
try{
if(source==null){
source=new SQLServerDataSource();
source.setDatabaseName(strDataName);
source.setServerName(strHostAddress);
source.setPortNumber(intHostPort);
source.setUser(strUserName);
source.setPassword(strPassWord);
//file://source.setHostProcess(intMaxConnection);
}
}catch(Exception e){
System.out.println("open database error:"+e.getMessage());
}
}
/**
* @param executeQuery查询数据库方法
* @param 每条ArrayList记录存为String[] 数组
* @return ArrayList
* @exception SQLException
*/
public ArrayList executeQuery(String strSql) throws SQLException {
rs=null;
try{
con=source.getConnection();
stmt=con.createStatement();
rs=stmt.executeQuery(strSql);
ResultSetMetaData rsmd=rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
file://判断是否为空
if(!ArrayRs.isEmpty()){
ArrayRs.clear();
}
/*
* 将每条记录写入数组
* 将数组放在ArrayList里
*/
while(rs.next()){
String[] strArrayTemp=new String[numberOfColumns];
for(int i=0;i<numberOfColumns;i++){
if(rs.getObject(i+1)==null){
strArrayTemp[i]= "";
}else{
strArrayTemp[i]=rs.getObject(i+1).toString();
}
}
ArrayRs.add(strArrayTemp);
}
return (ArrayList)ArrayRs.clone();
}catch(Exception e){
System.out.println("query error:" + e.getMessage());
}finally{
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
}
return ArrayRs;
}
}这个是我使用的的错误
DBConnect con=new DBConnect();
String sql="select * from user";
try {
System.out.print( con.executeQuery(sql));
System.out.print( con.executeQuery(sql).get(0));
} catch (SQLException e) {
e.printStackTrace();
}
严重: Servlet.service() for servlet action threw exception
java.lang.NoClassDefFoundError: com/merant/jdbcspy/SpyLogger
at com.microsoft.jdbcx.base.BaseDataSource.getConnection(Unknown Source) 这里要加 get和set的方法么?!
at service.db.DBConnect.executeQuery(DBConnect.java:54)
at service.form.LoginForm.validate(LoginForm.java:51)
还有就是ArrayList executeQuery返回ArrayRs这个。
例如数据库里面有两个项,name,password。我怎么使用ArrayRs区分他们呢?!
晚一点我贴出添加,修改,删除的代码的。如果大家需要的话
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货