日期:2014-05-16 浏览次数:20523 次
import java.sql.*; import java.util.*; import java.io.*; import java.text.*; public class getAccessDB { public static void getInsert(Connection con){ String sql = "INSERT INTO basic VALUES ('L004','兰博基尼','2010/5/9','555555')"; try{ PreparedStatement ps = con.prepareStatement(sql); ps.executeUpdate(); //con.close(); }catch(Exception e){ e.getMessage(); } } public static Connection connect(){ try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:myaccdb"; Connection con = DriverManager.getConnection(url); return con; }catch(Exception e){ System.out.println("wrong"); e.getCause(); return null; } } public static void getSelect(Connection con,Statement st){ String sql = "select * from basic"; try{ ResultSet rs = st.executeQuery(sql); while(rs.next()){ for(int i = 1; i <= 4; i++){ System.out.print(rs.getString(i)+" "); } System.out.println(); } }catch(Exception e){ System.out.println("sssss"); e.getCause(); } } public static void close(Connection con)throws SQLException{ con.close(); } public static void main(String[] args) { // TODO Auto-generated method stub try{ Connection con = connect(); Statement st = con.createStatement(); System.out.println("原始数据:"); getSelect(con,st); System.out.println("插入数据为:'L004','兰博基尼','2010/5/9','555555'"); getInsert(con); System.out.println("插入后:"); getSelect(con,st); close(con); }catch(Exception e){ System.out.println("wrong"); e.getCause(); } } }