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";

        }

}
View Code

 

执行结果:

 

调用方式:

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();
     }
    
    
}

 

posted @ 2023-05-01 13:31  有翅膀的大象  阅读(254)  评论(0编辑  收藏  举报