Java 错误 - net.ucanaccess.jdbc.UcanaccessSQLException:格式错误的字符串:'
Posted
技术标签:
【中文标题】Java 错误 - net.ucanaccess.jdbc.UcanaccessSQLException:格式错误的字符串:\'【英文标题】:Java Error - net.ucanaccess.jdbc.UcanaccessSQLException: malformed string: 'Java 错误 - net.ucanaccess.jdbc.UcanaccessSQLException:格式错误的字符串:' 【发布时间】:2014-10-04 10:18:46 【问题描述】:我在使用 uCanaccess 检索我保存在桌面中的数据库时出错。
我的代码如下:
package Week11Package;
导入 java.util.Scanner; 导入 java.sql.*;
公共类 dbTest1
static Scanner input = new Scanner (System.in);
static String url;
static Connection aConnection;
static Statement aStatement;
static boolean gotIt = false;
public static void main(String[] args)
dbTest1.initialize();
public static void initialize()
//establish the DB connection.
url = "jdbc:odbc:MS Access Database;DBQ=.//Teams.accdb";
try
//load the jdbc - odbc bridge for Windows
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
//create a connection instance
aConnection = DriverManager.getConnection("jdbc:ucanaccess:///Users/Vince/Desktop/Teams.accdb");
//create statement object instance for this connection
aStatement = aConnection.createStatement();
String sqlQuery = "SELECT PlayerID, PlayerName, TeamID " +
"FROM PLAYER"+"'";
ResultSet rs = aStatement.executeQuery (sqlQuery);
gotIt = rs.next();
if (gotIt)
System.out.println("Connected to DB & found Data!!!");
System.out.println("Which Player ID are you looking for?");
String stringpID = input.next();
boolean found = false;
while(gotIt)
//extract the data
String pID = rs.getString (1);
if (pID.equals(stringpID))
found = true;
String pName = rs.getString (2);
String tID = rs.getString (3);
System.out.println(pID+" " +pName+ " "+tID);
gotIt = rs.next();
if (found)
System.out.println("The player ws found");
aStatement.close();
aConnection.close();
catch (ClassNotFoundException e)
System.err.println(e);
catch (SQLException e)
System.err.println(e);
如果有任何解决此错误的建议,我将不胜感激。
谢谢, 文斯
【问题讨论】:
请发布完整的堆栈跟踪。您的sqlQuery
字符串末尾还有一个引号 ('),这不是问题吗?
正如 Cédric 所说,您的 sqlQuery 末尾的引号不正确,因此您会收到一条消息“格式错误的字符串”。
【参考方案1】:
首先,导入您的库(lib 文件夹中的 jar 文件)。 http://sourceforge.net/projects/ucanaccess/files/UCanAccess-2.0.9.1-bin.zip/download
然后检查一下。这可能会有所帮助。
Connection a = DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\Vince\\Desktop\\Teams.accdb");
Statement s = a.createStatement();
String sqlQuery = "SELECT PlayerID, PlayerName, TeamID " + "FROM PLAYER"+"'";
ResultSet rs = s.executeQuery(sqlQuery);
while(rs.next())
System.out.println("Connected to DB & found Data!!!");
【讨论】:
以上是关于Java 错误 - net.ucanaccess.jdbc.UcanaccessSQLException:格式错误的字符串:'的主要内容,如果未能解决你的问题,请参考以下文章
Android Studio:net.ucanaccess.jdbc.UcanaccessSQLException:错误文件不存在
java.lang.ClassNotFoundException:net.ucanaccess.jdbc.ucanaccessDriver
net.ucanaccess.jdbc.UcanaccessSQLException 游标状态无效