JAVA的Jdbc连接Access数据库
Eclipse加入Access_JDBC30.jar:
程序如下:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class conn { public static String connct() { try{ //Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ); //Connection conn=DriverManager.getConnection("jdbc:odbc:MondrianFoodMart","user","password"); Class.forName("com.hxtt.sql.access.AccessDriver"); Connection conn = DriverManager.getConnection("jdbc:Access:///D:/Mondrian/foodmart/access/MondrianFoodMart.mdb","sa","123456"); Statement sql=conn.createStatement(); System.out.print("数据库连接成功"); ResultSet rs=sql.executeQuery("select * from product"); while(rs.next()){ System.out.println(rs.getString("brand_name")); //获取brand_name列数据值 System.out.println(rs.getString(2)); //获取数据表中第二列数据值 } return "ok"; } catch(Exception e) { } return "no"; } }
执行结果:
调用方式:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.*; public class test { // conn n=new conn(); public static void main(String[] args) { String s= conn.connct(); } }
,Best Wish 不负年华