当前位置: 技术问答>java相关
请赐教:关于Java于Access数据库操作的问题
来源: 互联网 发布时间:2015-04-23
本文导语: 怎么使用java从Access表中读取数据??请给个 程序例子,谢谢!!! | 步骤是一样的。 import java.sql.*; Connection connection=null; PreparedStatement statement=null; try{ Class.forName("sun.jd...
怎么使用java从Access表中读取数据??请给个
程序例子,谢谢!!!
程序例子,谢谢!!!
|
步骤是一样的。
import java.sql.*;
Connection connection=null;
PreparedStatement statement=null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection=DriverManager.getConnection("jdbc:odbc:testAccess","sa","");
Enumeration eDri = DriverManager.getDrivers();
while(eDri.hasMoreElements())
System.out.println("驱动程序:" + eDri.nextElement().toString());
}
catch(Exception e)
{
e.printStackTrace();
}
try{
String sql="select * from people";
statement=connection.prepareStatement(sql);
ResultSet result=statement.executeQuery();
while(result.next())
{
int nid=result.getInt("id");
String strid=new String("id "+nid);
System.out.println(strid);
String name=result.getString("name");
System.out.println("name "+name);
String sex=result.getString("sex");
System.out.println("sex "+sex);
// int age=result.getInt("age");
// String strAge=new String(""+age);
// System.out.println("age "+strAge);
String phone=result.getString("phone");
System.out.println("phone "+phone);
String other=result.getString("other");
System.out.println("other "+other);
}
result.close();
statement.close();
}catch(Exception e)
{
System.out.println(e.toString());
}
import java.sql.*;
Connection connection=null;
PreparedStatement statement=null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection=DriverManager.getConnection("jdbc:odbc:testAccess","sa","");
Enumeration eDri = DriverManager.getDrivers();
while(eDri.hasMoreElements())
System.out.println("驱动程序:" + eDri.nextElement().toString());
}
catch(Exception e)
{
e.printStackTrace();
}
try{
String sql="select * from people";
statement=connection.prepareStatement(sql);
ResultSet result=statement.executeQuery();
while(result.next())
{
int nid=result.getInt("id");
String strid=new String("id "+nid);
System.out.println(strid);
String name=result.getString("name");
System.out.println("name "+name);
String sex=result.getString("sex");
System.out.println("sex "+sex);
// int age=result.getInt("age");
// String strAge=new String(""+age);
// System.out.println("age "+strAge);
String phone=result.getString("phone");
System.out.println("phone "+phone);
String other=result.getString("other");
System.out.println("other "+other);
}
result.close();
statement.close();
}catch(Exception e)
{
System.out.println(e.toString());
}