首页新闻找找看学习计划

eclipse中 连接access总是报错

0
悬赏园豆:5 [待解决问题]

以下是源代码:

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBDemo {
Connection con;
Statement stm;
String sql;
ResultSet rs;

DBDemo() { //构造方法进行初始化
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
jdbc:odbc:driver = { Microsoft Access Driver (*.mdb); 

String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\实例.MDB";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(url);
System.out.println(conn);
} catch(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch(ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void insert() { //插入数据
sql = "insert into 学生表(学号,学生姓名,身份证号,性别,年级,专业)values('20030004','王五','37021545678','男','2016','专业')";
try {
int i = stm.executeUpdate(sql);
// System.out.println("i="+i);;
if(i > 0) { //i的值表示语句操作的记录数量,i=0表示未进行数据库操作
System.out.println("添加成功!");
}
} catch(SQLException e) {
// TODO Auto-generated catch block
System.out.println("添加失败!");
// e.printStackTrace();
}
}




public static void main(String[] args) {
DBDemo d = new DBDemo();
d.insert();



try {
d.rs.close();
d.stm.close();
d.con.close();
} catch(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

 

报错:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error on token(s), misplaced construct(s)
Syntax error on token "Access", , expected
Syntax error on token "*", invalid Expression

at DBDemo.<init>(DBDemo.java:20)
at DBDemo.main(DBDemo.java:54)

问题补充:

经过不断百度,程序修改为:

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBDemo {
Connection con;
Statement stm;
String sql;
ResultSet rs;

DBDemo() { //构造方法进行初始化
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\实例.MDB";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(url);
System.out.println(conn);
} catch(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch(ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void insert() { //插入数据
sql = "insert into 学生表(学号,学生姓名,身份证号,性别,年级,专业)values('20030004','王五','37021545678','男','2016','专业')";
try {
int i = stm.executeUpdate(sql);
// System.out.println("i="+i);;
if(i > 0) { //i的值表示语句操作的记录数量,i=0表示未进行数据库操作
System.out.println("添加成功!");
}
} catch(SQLException e) {
// TODO Auto-generated catch block
System.out.println("添加失败!");
// e.printStackTrace();
}
}




public static void main(String[] args) {
DBDemo d = new DBDemo();
d.insert();



try {
d.rs.close();
d.stm.close();
d.con.close();
} catch(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

但依旧报错:

Exception in thread "main" java.lang.NullPointerException
at sun.jdbc.odbc.JdbcOdbcDriver.initialize(JdbcOdbcDriver.java:436)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:153)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at DBDemo.<init>(DBDemo.java:24)
at DBDemo.main(DBDemo.java:54)

gq001的主页 gq001 | 初学一级 | 园豆:197
提问于:2016-11-06 00:27
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册