Skip to content

Instantly share code, notes, and snippets.

@RELATO
Created October 23, 2015 11:32
Show Gist options
  • Save RELATO/7afcd59a5f209e7e91c9 to your computer and use it in GitHub Desktop.
Save RELATO/7afcd59a5f209e7e91c9 to your computer and use it in GitHub Desktop.
Java program to update _seq hibernate sequence table style
package br.com.relato.util;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class AtualizaSeq {
/**
* @param args
*/
final String DATABASE_PATH = "jdbc:mysql://192.168.0.209/apo";
final String DATABASE_USER = "root";
final String DATABASE_PASS = "[password]";
public void atualizaSeqDatabase(){
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Connection c = null;
try {
c = DriverManager.getConnection(DATABASE_PATH, DATABASE_USER, DATABASE_PASS);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(null!=c){
try {
DatabaseMetaData metadata = c.getMetaData();
ResultSet r = metadata.getTables(null, null, null, null);
Map<String,Integer> mapaUpdSeqs = new LinkedHashMap<String, Integer>();
Map<String,Integer> mapaSeqs = new LinkedHashMap<String, Integer>();
while(r.next()){
String nomedatabela = null!= r.getObject(3)?r.getString(3):"";
if(nomedatabela.trim().endsWith("_seq")){
System.out.println("SEQ Tabela:'"+nomedatabela+"'");
mapaSeqs.put(nomedatabela, new Integer(0));
}
}
r.close();
Iterator<String> itSeq = mapaSeqs.keySet().iterator();
while(itSeq.hasNext()){
String tabela_seq = itSeq.next();
String tabela_orig = tabela_seq.lastIndexOf("_seq") > -1 ? tabela_seq.substring(0,tabela_seq.lastIndexOf("_seq")): "";
Integer idpk = new Integer(0);
if(!"".equals(tabela_orig)){
try{
ResultSet rscampopk = metadata.getPrimaryKeys(null, null, tabela_orig);
if(rscampopk.next()){
String pk_campo = rscampopk.getString(4);
String sel_max = "select max("+pk_campo+") from "+tabela_orig+" ";
PreparedStatement ps = c.prepareStatement(sel_max);
System.out.println("pk_campo: '"+pk_campo+"' -- tabela_orig: '"+tabela_orig+"'");
ResultSet rspk = ps.executeQuery();
if(rspk.next()){
idpk = null!=rspk.getObject(1)?Cast.toInt(rspk.getObject(1)):new Integer(0);
}
rspk.close();
mapaUpdSeqs.put(tabela_seq, idpk);
}
rscampopk.close();
}catch(SQLException s){
System.out.println("Tabela nao existe: '"+tabela_orig+"'");
//s.printStackTrace();
}
}
}
Iterator<String> itUpdSeq = mapaUpdSeqs.keySet().iterator();
while(itUpdSeq.hasNext()){
String tabela = itUpdSeq.next();
Integer next_value = mapaUpdSeqs.get(tabela);
String select_seq = " select * from "+tabela+" ";
Statement st_check = c.createStatement();
ResultSet check_seq = st_check.executeQuery(select_seq);
boolean updcheck = check_seq.next();
check_seq.close();
if(updcheck){
String update_seq = " update "+tabela+" set next_value = ?";
PreparedStatement ps_seq = c.prepareStatement(update_seq);
ps_seq.setInt(1, next_value);
ps_seq.executeUpdate();
ps_seq.close();
}else{
String insert_seq = " insert into "+tabela+" values (?)";
PreparedStatement ps_seq = c.prepareStatement(insert_seq);
ps_seq.setInt(1, next_value);
ps_seq.executeUpdate();
ps_seq.close();
}
System.out.println("Tabela ok: '"+tabela+"' -- seq:"+next_value);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
c.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
AtualizaSeq as = new AtualizaSeq();
as.atualizaSeqDatabase();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment