java cannot get the latest inserted record when reading access database through access? JDBC 30

Posted by souravsasi123 on Tue, 26 Nov 2019 22:37:00 +0100

1. Write a circular program to read the latest row of data in the database every few seconds

The method of connecting access database and the information of query. Then open a timing to remove.

 

package javacommon.util;


import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import java.sql.SQLException;
import java.sql.Statement;

import com.ybb.DBConnection;
/**
 * 
 * @author ybb
 * Connect to Access database
 * java Get access database data through access? JDBC 30, unable to get the latest data
 */
public class AccessDBUtil {
	
	private static  final String DRIVER="com.hxtt.sql.access.AccessDriver";
	private static final String URL="jdbc:Access:///G:/ceshi/ceshi.mdb";
	private static final String USER="";
	private static final String PASSWORD="";
	
	public static Connection getConnection(){
		try {
			Class.forName(DRIVER);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		try {
			Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);
			return connection;
		} catch (SQLException e) {
			return null;
		}
	}
	
	public static void colseConnection(Connection conn){
		try {
			if(conn!=null&&!conn.isClosed()){
				conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void myclose(Connection con,PreparedStatement ps){
		try {
			if (con!=null&&!con.isClosed()) {
				con.close();
			}
			if (ps!=null) {
				ps.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void myclose(Connection con,PreparedStatement ps,ResultSet rs){
		try {
			if (con!=null&&!con.isClosed()) {
				con.close();
			}
			if (ps!=null) {
				ps.close();
			}
			if (rs!=null) {
				rs.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/** 
	 * test 
	 * */  
	public static void main(String[] args){  
		Connection connnect = null;
		while(true){
			try{  
			    connnect = AccessDBUtil.getConnection();
				if(connnect!=null){  
					System.out.println(connnect+"\n Successful connection");  
				}else{  
					System.out.println("connection failed");  
				}  
				 Statement stat = connnect.createStatement();
				 ResultSet rs = stat.executeQuery("select * from CYJ_PD_QTJL");
				 if(rs!=null){  
						while(rs.next()){  
							System.out.println(rs.getString(1)+"\t"+rs.getString(2));    
						}  
				}  
				Thread.sleep(5000);
			}catch(Exception e){  
				e.printStackTrace();  
			}finally{  
				colseConnection(connnect);//Close links  
			}  
		}
	}  
	
}

  

 

 

2. When manually entering the CYJ? PD? Qtjl table in the corresponding access database

When adding data, the data just added cannot be queried. This program needs to be restarted to be queried.

3. To solve this problem, replace the driver. Perfect solution with ODBC connection.

package com.ybb;
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.PreparedStatement;  
import java.sql.ResultSet;  
import java.util.Properties;  

/**
 * 
 * @author ybb
 * Connect access data 
 * Get access database data, you can get the latest data
 * 2019 2:48:33 PM, April 16, 2016
 */
public class DBConnection { 
	/** 
	 * This method is used to connect to the database 
	 * @param db:Data source name 
	 * */  
	public static Connection getDBConnection(){  
		try{  
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//Registration driven  
			//The default database code in Access is GBK, and the local project is UTF-8. Without transcoding, there will be garbled code  
			Properties p = new Properties();  
			p.put("charSet", "GBK");  
			Connection connect= DriverManager.getConnection("jdbc:odbc:ceshi",p); 
			return connect;
		}catch(Exception e){  
			e.printStackTrace();  
			return null;
		}  
	}  

	/** 
	 * This method is used to execute SQL and return the result set 
	 * */  
	public static ResultSet selectQuery(String sql){  
		try{  
			PreparedStatement stmt = getDBConnection().prepareStatement(sql);  
			ResultSet rs = stmt.executeQuery();//Implementation of SQL  
			return rs;  
		}catch(Exception e){  
			e.printStackTrace();  
			return null; 
		}  
	}  


	/** 
	 * test 
	 * */  
	public static void main(String[] args){  
		while(true){
			try{  
			    Connection connnect = DBConnection.getDBConnection();  
				if(connnect!=null){  
					System.out.println(connnect+"\n Successful connection");  
				}else{  
					System.out.println("connection failed");  
				}  
				ResultSet rs1 = selectQuery("select * from CYJ_PD_QTJL");  
				if(rs1!=null){  
					while(rs1.next()){  
						System.out.println(rs1.getString(1)+"\t"+rs1.getString(2));    
					}  
				}  
				Thread.sleep(5000);
			}catch(Exception e){  
				e.printStackTrace();  
			}finally{  
//				closeConn(); / / close the link  
			}  
		}
	}  
}

4. At this time, manually add data to CYJ ﹣ PD ﹣ qtjl table. The data just added can be queried immediately.

Topics: Java SQL Database JDBC