java不用odbc设置数据源连接access编程问题,数据库没有问题,输出不了,求修改
importjava.sql.Connection;importjava.sql.Statement;importjava.sql.ResultSet;importjav...
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
public class MyExample {
public static void main(String []args) throws Exception {
String url="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=book.mdb";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
String command = "select * from book where bno='1'";
ResultSet rs = stmt.executeQuery(command);
String x1=rs.getString(1);String x2= rs.getString(2);String x3=rs.getString(3);
String x4= rs.getString(4);float x5=rs.getFloat(5);String x6=rs.getString(6);
while(rs.next()) {
System.out.print(" " + x1 + " ");
System.out.print(" " + x2 + " ");
System.out.print(" " + x3 + " ");
System.out.print(" " + x4 + " ");
System.out.print(" " + x5 + " ");
System.out.print(" " + x6 + " ");conn.close();
}
}
catch(Exception e) {
e.printStackTrace();
}
}
} 展开
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
public class MyExample {
public static void main(String []args) throws Exception {
String url="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=book.mdb";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
String command = "select * from book where bno='1'";
ResultSet rs = stmt.executeQuery(command);
String x1=rs.getString(1);String x2= rs.getString(2);String x3=rs.getString(3);
String x4= rs.getString(4);float x5=rs.getFloat(5);String x6=rs.getString(6);
while(rs.next()) {
System.out.print(" " + x1 + " ");
System.out.print(" " + x2 + " ");
System.out.print(" " + x3 + " ");
System.out.print(" " + x4 + " ");
System.out.print(" " + x5 + " ");
System.out.print(" " + x6 + " ");conn.close();
}
}
catch(Exception e) {
e.printStackTrace();
}
}
} 展开
4个回答
展开全部
字段的数据类型有没有与用rs.getXXX获取的值不是同类型的,bno这个字段是不是数字的?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我想问一下,你的Access数据库文件的存放目录的相对路径是?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果是报exception 先贴打出来的stacktrace
如果是输出结果不正确 在rs = stmt.xxxx 这一句打断点往里面跟
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
public class MyExample {
public static void main(String[] args) throws Exception {
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=book.mdb";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(url);
stmt = conn.createStatement();
String command = "select * from book where bno='1'";
rs = stmt.executeQuery(command);
while (rs.next()) {
String x1 = rs.getString(1);
String x2 = rs.getString(2);
String x3 = rs.getString(3);
String x4 = rs.getString(4);
float x5 = rs.getFloat(5);
String x6 = rs.getString(6);
System.out.print(" " + x1 + " ");
System.out.print(" " + x2 + " ");
System.out.print(" " + x3 + " ");
System.out.print(" " + x4 + " ");
System.out.print(" " + x5 + " ");
System.out.print(" " + x6 + " ");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null && !rs.isClosed()) {
rs.close();
}
if (stmt != null && !stmt.isClosed()) {
stmt.close();
}
if (conn != null && !conn.isClosed()) {
conn.close();
}
}
}
}
给你改了个版本 应该能跑了
如果是输出结果不正确 在rs = stmt.xxxx 这一句打断点往里面跟
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
public class MyExample {
public static void main(String[] args) throws Exception {
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=book.mdb";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(url);
stmt = conn.createStatement();
String command = "select * from book where bno='1'";
rs = stmt.executeQuery(command);
while (rs.next()) {
String x1 = rs.getString(1);
String x2 = rs.getString(2);
String x3 = rs.getString(3);
String x4 = rs.getString(4);
float x5 = rs.getFloat(5);
String x6 = rs.getString(6);
System.out.print(" " + x1 + " ");
System.out.print(" " + x2 + " ");
System.out.print(" " + x3 + " ");
System.out.print(" " + x4 + " ");
System.out.print(" " + x5 + " ");
System.out.print(" " + x6 + " ");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null && !rs.isClosed()) {
rs.close();
}
if (stmt != null && !stmt.isClosed()) {
stmt.close();
}
if (conn != null && !conn.isClosed()) {
conn.close();
}
}
}
}
给你改了个版本 应该能跑了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询