jdbc连接sql2000问题

q409194872 2008-04-22 11:30:40
首先:我测试了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[]){ }

出现一大队错误 ,我无奈了
...全文
128 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoban0514 2008-04-22
  • 打赏
  • 举报
回复
看你的类没什么问题,
比最好把你那个调用的类再发上来看一下。
  • 打赏
  • 举报
回复
什么错误
海边岩石 2008-04-22
  • 打赏
  • 举报
回复
在这个类里加一个main方法,
在main方法里创建这个类的实例,然后用这个实例分别调用
这个类的成员函数来做测试就行了。

81,114

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧