protected boolean alreadyin(string tname,string colname, string value)
{
int result;
resultset rst=null;
try {
//执行sql语句
string query = "select "+colname+" from "+tname+" where "+colname+"='"+value+"'";
statement statement = connformax.createstatement();
rst = statement.executequery( query );
if(rst.next())
{
statement.close();
rst.close();
return true;
}
}
catch ( sqlexception sqlex ) {
sqlex.printstacktrace();
return false;
}
return false;
}
protected int getidfromnumber(string tname,string colname, string value)
{
int result;
resultset rst=null;
try {
connection conn= drivermanager.getconnection( destpara.geturl(), destpara.getusername(),destpara.getpassword());
string query = "select id,"+colname+" from "+tname+" where "+colname+"='"+value+"'";
system.out.println(query);
statement statement = conn.createstatement();
rst = statement.executequery( query );
if(rst.next())
{
return rst.getint("id");
}
}
catch ( sqlexception sqlex ) {
sqlex.printstacktrace();
return 0;
}
return 0;
}
/**
* 得到某个表中的最大的id号
*/
protected int getmax(string tname)
{
int result;
resultset rst=null;
try {
//执行sql语句
string query = "select max(id) from "+tname;
statement statement = connformax.createstatement();
rst = statement.executequery( query );
if(rst.next())
{
return rst.getint(1)+1;
}
}
catch ( sqlexception sqlex ) {
sqlex.printstacktrace();
return 0;
}
return 1;
}
/**
* 执行某一段sql语句
*/
public static void execute(connpara connpara,string stmt) throws sqlexception
{
connection conn=null;
preparedstatement ps = null;
try {
conn=drivermanager.getconnection( connpara.geturl(), connpara.getusername(), connpara.getpassword());
system.out.println(stmt);
ps = conn.preparestatement(stmt);
ps.executeupdate();
} catch (exception e) {
e.printstacktrace();
system.out.println(e.getmessage());
} finally {
if (ps!=null) ps.close();
if (conn!=null)conn.close();
}
}
public static void main(string argc[])
{
dbinput copydb=new dbinput();
copydb.dbinit();
copydb.copyproduct();
}
}
问题:
1) access数据库不能直接由jdbc读写,解决办法是先把access配置在odbc中,然后再通过odbc来操作access数据库。
2) 执行时找不到com.microsoft.jdbc.sqlserver.sqlserverdriver类,这是因为在运行这个类时,要下载微软的jdbc包,这个包中有三个文件: msbase.jar,mssqlserver.jar,msutil.jar,把这三个文件包含进去,就不会有问题了。
闽公网安备 35060202000074号