jdbc连接sql2000问题
首先:我测试了java环境变量测试成功了,我也将三个jar放入了我的lib里并设classpath了
这是我在该论坛上求得测试jdbc连接sql2000的java文件
代码如是:
package com.fuyou;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
public class DBConnection {
private String Driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String user = "sa";
private String password = "fuyou";
private String url = "jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=pubs";
private Connection con = null;
private Statement stmt = null;
public DBConnection() {
try {
Class.forName(Driver);
con = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("沒有找到驱动类!");
} catch (SQLException e) {
e.printStackTrace();
}
}
public Connection getConnection() {
try {
con = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
public Statement getStatement() { // 产生结果集可以滚动,可以更新,但不敏感
if (this.con == null) {
this.con = getConnection();
}
try {
stmt = this.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}
public Statement getPreparedStatement(String sql) { // 预编译结果集可以滚动,可以更新,但不敏感
if (this.con == null) {
this.con = getConnection();
}
try {
stmt = this.con.prepareStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}
public ResultSet executeQuery(String sql) { // 查询方法
ResultSet rs = null;
if (this.con == null) {
this.con = getConnection();
}
stmt = getStatement();
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public int executeUpdate(String sql) { // 更新方法,返回更新的记录数
Statement stmt = null;
int num = 0;
if (this.con == null) {
this.con = getConnection();
stmt = getStatement();
}
try {
this.con.setAutoCommit(false);
num = stmt.executeUpdate(sql);
con.commit();
con.setAutoCommit(true);
} catch (SQLException e) {
e.printStackTrace();
try {
con.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
return num;
}
public void closeResultset(ResultSet rs) { // 关闭结果集
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void closeStatement() {// 关闭stmt
if (this.stmt != null) {
try {
this.stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void closeConection() { // 关闭con
if (this.con != null) {
try {
this.con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
可是在jceator里运行时显示为:
--------------------配置: <--------------------
java.lang.NoSuchMethodError: main
Exception in thread "main"
处理已完成。
我在开头加上:public static void main(String args[]){ }
出现一大队错误 ,我无奈了