GWT 和 SQL - 没有合适的驱动程序
GWT and SQL - No Suitable Driver
我目前在使用 GWT 和 SQL 时遇到了一个非常奇怪的问题。
我正在尝试让我的 GWT 程序与我的 SQL 服务器通信。我从网上拿了几个例子,它们都会因以下错误而终止:
SEVERE: No suitable driver found for jdbc:mysql://localhost:3306/table
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/table
经过数小时的绞尽脑汁,我终于想出了以下 有效的代码。 运行 以下将愉快地将 MySQL 版本输出到控制台:
public static void printVersion() {
Connection con = null;
Statement st = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/table";
String user = "username";
String password = "password";
try {
con = DriverManager.getConnection(url, user, password);
st = con.createStatement();
rs = st.executeQuery("SELECT VERSION()");
if (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(Version.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
} finally {
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (con != null) {
con.close();
}
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(Version.class.getName());
lgr.log(Level.WARNING, ex.getMessage(), ex);
}
}
}
但是,当我将方法从将字符串输出到控制台稍微更改为将字符串返回到调用函数时,我又开始收到 "No suitable driver found" 错误。
我所做的更改非常微妙;这是差异:
1c1
< public static String getVersion() {
---
> public static void printVersion() {
10d9
< String rtn = null;
18c17
< rtn = rs.getString(1);
---
> System.out.println(rs.getString(1));
42d40
< return rtn;
调用函数只是从 Version.printVersion()
更改为 String str = Version.getVersion()
。
我在 PHP 方面相当有经验,现在正准备跳转到 GWT/Java。我到处搜索,但没能找出为什么它不能正常工作。许多教程和预先存在的代码都让我失望了,所以我在这里问。
我 do 在我的类路径中有 MySQL JDBC 驱动程序,并且我尝试了几种使用 Class.forName("com.mysql.jdbc.Driver").newInstance();
加载它的排列方式,但是无济于事
我确定我错过了一些非常愚蠢的东西,但我太缺乏经验,不知道是什么。
老实说,我不知道为什么 Class.forName("com.mysql.jdbc.Driver").newInstance();
以前不工作。我尝试了几种方法,其中 none 有效。 (我已经删除了我的非工作工作,所以我无法比较为什么它不起作用)不过我刚才在这个例子中尝试过它并且在添加时它似乎确实有效。这是差异:
1c1
< public static String getVersion() {
---
> public static void printVersion() {
10d9
< String rtn = null;
13d11
< Class.forName("com.mysql.jdbc.Driver").newInstance();
19c17
< rtn = rs.getString(1);
---
> System.out.println(rs.getString(1));
43d40
< return rtn;
基本上只需在初始化连接之前添加 Class.forName("com.mysql.jdbc.Driver").newInstance();
。
我完全不知道为什么如果我只是打印输出但 不会 工作而 会 在没有该语句的情况下工作如果我要返回输出。谁能向我描述,谁就能获得超级奖励积分。
编辑:
搞砸了 diff
- 您还需要为 InstantiationException
、IllegalAccessException
和 ClassNotFoundException
添加异常处理程序。不过,您的 IDE 应该会提示您这一点。
我目前在使用 GWT 和 SQL 时遇到了一个非常奇怪的问题。
我正在尝试让我的 GWT 程序与我的 SQL 服务器通信。我从网上拿了几个例子,它们都会因以下错误而终止:
SEVERE: No suitable driver found for jdbc:mysql://localhost:3306/table
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/table
经过数小时的绞尽脑汁,我终于想出了以下 有效的代码。 运行 以下将愉快地将 MySQL 版本输出到控制台:
public static void printVersion() {
Connection con = null;
Statement st = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/table";
String user = "username";
String password = "password";
try {
con = DriverManager.getConnection(url, user, password);
st = con.createStatement();
rs = st.executeQuery("SELECT VERSION()");
if (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(Version.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
} finally {
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (con != null) {
con.close();
}
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(Version.class.getName());
lgr.log(Level.WARNING, ex.getMessage(), ex);
}
}
}
但是,当我将方法从将字符串输出到控制台稍微更改为将字符串返回到调用函数时,我又开始收到 "No suitable driver found" 错误。
我所做的更改非常微妙;这是差异:
1c1
< public static String getVersion() {
---
> public static void printVersion() {
10d9
< String rtn = null;
18c17
< rtn = rs.getString(1);
---
> System.out.println(rs.getString(1));
42d40
< return rtn;
调用函数只是从 Version.printVersion()
更改为 String str = Version.getVersion()
。
我在 PHP 方面相当有经验,现在正准备跳转到 GWT/Java。我到处搜索,但没能找出为什么它不能正常工作。许多教程和预先存在的代码都让我失望了,所以我在这里问。
我 do 在我的类路径中有 MySQL JDBC 驱动程序,并且我尝试了几种使用 Class.forName("com.mysql.jdbc.Driver").newInstance();
加载它的排列方式,但是无济于事
我确定我错过了一些非常愚蠢的东西,但我太缺乏经验,不知道是什么。
老实说,我不知道为什么 Class.forName("com.mysql.jdbc.Driver").newInstance();
以前不工作。我尝试了几种方法,其中 none 有效。 (我已经删除了我的非工作工作,所以我无法比较为什么它不起作用)不过我刚才在这个例子中尝试过它并且在添加时它似乎确实有效。这是差异:
1c1
< public static String getVersion() {
---
> public static void printVersion() {
10d9
< String rtn = null;
13d11
< Class.forName("com.mysql.jdbc.Driver").newInstance();
19c17
< rtn = rs.getString(1);
---
> System.out.println(rs.getString(1));
43d40
< return rtn;
基本上只需在初始化连接之前添加 Class.forName("com.mysql.jdbc.Driver").newInstance();
。
我完全不知道为什么如果我只是打印输出但 不会 工作而 会 在没有该语句的情况下工作如果我要返回输出。谁能向我描述,谁就能获得超级奖励积分。
编辑:
搞砸了 diff
- 您还需要为 InstantiationException
、IllegalAccessException
和 ClassNotFoundException
添加异常处理程序。不过,您的 IDE 应该会提示您这一点。