当前位置: 技术问答>java相关
请各位帮忙看看这段代码。。。
来源: 互联网 发布时间:2015-11-21
本文导语: 下面这段代码编译出错,请教各位如何改正?谢谢! import java.sql.*; public class DBConn { String DBDriver = "jdbc:mysql://localhost/cwt"; Connection conn = null; ResultSet rs = null; public DBConn { try { //加载...
下面这段代码编译出错,请教各位如何改正?谢谢!
import java.sql.*;
public class DBConn {
String DBDriver = "jdbc:mysql://localhost/cwt";
Connection conn = null;
ResultSet rs = null;
public DBConn {
try {
//加载数据库驱动程序
Class.forName(DBDriver);
}
catch(java.lang.ClassNotFoundException e) {
System.err.print("DBConn (): " + e.getMessage());
}
}
public ResultSet executeQuery(String sql) {
rs = null;
}
catch(SQLExcption ex) {
System.err.print("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
}
错误信息:
D:tomcatjakarta-tomcat-3.3.1webappsROOTcwtcwtDBConn.java:6: expected
public DBConn {
^
D:tomcatjakarta-tomcat-3.3.1webappsROOTcwtcwtDBConn.java:23: 'class' or 'interface' expected
}
^
D:tomcatjakarta-tomcat-3.3.1webappsROOTcwtcwtDBConn.java:23: 'class' or 'interface' expected
}
^
3 errors
import java.sql.*;
public class DBConn {
String DBDriver = "jdbc:mysql://localhost/cwt";
Connection conn = null;
ResultSet rs = null;
public DBConn {
try {
//加载数据库驱动程序
Class.forName(DBDriver);
}
catch(java.lang.ClassNotFoundException e) {
System.err.print("DBConn (): " + e.getMessage());
}
}
public ResultSet executeQuery(String sql) {
rs = null;
}
catch(SQLExcption ex) {
System.err.print("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
}
错误信息:
D:tomcatjakarta-tomcat-3.3.1webappsROOTcwtcwtDBConn.java:6: expected
public DBConn {
^
D:tomcatjakarta-tomcat-3.3.1webappsROOTcwtcwtDBConn.java:23: 'class' or 'interface' expected
}
^
D:tomcatjakarta-tomcat-3.3.1webappsROOTcwtcwtDBConn.java:23: 'class' or 'interface' expected
}
^
3 errors
|
构造函数DBConn没有加括号应该为public DBConn(),在构造函数中为什么会存在execQuery()函数的声明呢?java不支持函数嵌套的。
|
DBConn是构造函数,语法有问题吧:public DBConn() {
|
catch(SQLExcption ex) {
System.err.print("aq.executeQuery: " + ex.getMessage());
}
怎么没有try
将"rs = null;"后面的“}”去掉
System.err.print("aq.executeQuery: " + ex.getMessage());
}
怎么没有try
将"rs = null;"后面的“}”去掉