3、访问Access 2003数据库,在Access数据库中创建学生表(T_Student)表并配置ODBC驱动源
代码如下:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestJDBC {
public void ChaXun()throws ClassNotFoundException, SQLException
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){
System.out.println("没有找到指定的驱动");
}
Connection conn=DriverManager.getConnection("jdbc:odbc:Student","sa","");
Statement stst=conn.createStatement();
ResultSet re=stst.executeQuery("select * from Student order by S_ID asc");
while(re.next())
{
System.out.println(re.getInt("S_ID")+","+re.getString("S_Name")+","+re.getString("S_Email")+","+re.getInt("S_Score"));
}
try{
if(re!=null)
re.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
if(conn!=null)
conn.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
public void Insert(int id,String name,String e_mail,int score) throws ClassNotFoundException, SQLException
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){
System.out.println("没有找到指定的驱动");
}
Connection conn=DriverManager.getConnection("jdbc:odbc:Student","sa","");
String sql="insert into Student(S_ID,S_Name,S_Email,S_Score)values(?,?,?,?)";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setInt(1,id);
ps.setString(2,name);
ps.setString(3,e_mail);
ps.setInt(4,score);
ps.executeUpdate();
try{
if(ps!=null)
ps.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
if(conn!=null)
conn.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
this.ChaXun();
}
}
public static void main(String []args) throws ClassNotFoundException, SQLException
{
TestJDBC te=new TestJDBC();
te.Insert(1004, "a","123@xx.com",89);
te.Insert(1006,"c","789@123.com",100);
te.Insert(1005,"b","456@123.com",99);
}
}
异常:
Exception in thread "main" java.sql.SQLException: [Microsoft][ODBC Microsoft Access 驱动程序] 找不到输出表 'Student' 。
为什么啊?ODBC数据源已经配好了,大虾帮帮忙!!!
[解决办法]
应该是你的数据源对应的数据库中没有创建 Student 这张表