1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
| package checkauthentication;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
// Database configuration
public static String url = "jdbc:mysql://localhost:3306/passwords";
public static String dbdriver = "com.mysql.jdbc.Driver";
public static String username = "root";
public static String password = "root";
public static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this thread has none yet
if (s == null) {
s = sessionFactory.openSession();
// Store it in the ThreadLocal variable
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
if (s != null)
s.close();
session.set(null);
}
static Connection conn;
static Statement st;
public static void setup(String sql) {
try {
createStatement();
st.executeUpdate(sql);
} catch (Exception e) {
System.err.println("Got an exception! ");
e.printStackTrace();
System.exit(0);
}
}
public static void createStatement() {
try {
Class.forName(dbdriver);
conn = DriverManager.getConnection(url, username, password);
st = conn.createStatement();
} catch (Exception e) {
System.err.println("Got an exception! ");
e.printStackTrace();
System.exit(0);
}
}
// Drop table if exists
public static void droptable(String sql) {
try {
createStatement();
st.executeUpdate(sql);
} catch (Exception e) {
}
}
public static void checkData(String sql) {
String[] starray = sql.split(" ");
System.out.println("\n******** Table: " + starray[starray.length-1] + " *******");
try {
createStatement();
ResultSet r = st.executeQuery(sql);
HibernateUtil.outputResultSet(r);
// conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void outputResultSet(ResultSet rs) throws Exception{
ResultSetMetaData metadata = rs.getMetaData();
int numcols = metadata.getColumnCount();
String[] labels = new String[numcols];
int[] colwidths = new int[numcols];
int[] colpos = new int[numcols];
int linewidth;
linewidth = 1;
for (int i = 0; i < numcols; i++) {
colpos[i] = linewidth;
labels[i] = metadata.getColumnLabel(i + 1); // get its label
int size = metadata.getColumnDisplaySize(i + 1);
if (size > 30 || size == -1)
size = 30;
int labelsize = labels[i].length();
if (labelsize > size)
size = labelsize;
colwidths[i] = size + 1; // save the column the size
linewidth += colwidths[i] + 2; // increment total size
}
StringBuffer divider = new StringBuffer(linewidth);
StringBuffer blankline = new StringBuffer(linewidth);
for (int i = 0; i < linewidth; i++) {
divider.insert(i, '-');
blankline.insert(i, " ");
}
// Put special marks in the divider line at the column positions
for (int i = 0; i < numcols; i++)
divider.setCharAt(colpos[i] - 1, '+');
divider.setCharAt(linewidth - 1, '+');
// Begin the table output with a divider line
System.out.println(divider);
// The next line of the table contains the column labels.
// Begin with a blank line, and put the column names and column
// divider characters "|" into it. overwrite() is defined below.
StringBuffer line = new StringBuffer(blankline.toString());
line.setCharAt(0, '|');
for (int i = 0; i < numcols; i++) {
int pos = colpos[i] + 1 + (colwidths[i] - labels[i].length()) / 2;
overwrite(line, pos, labels[i]);
overwrite(line, colpos[i] + colwidths[i], " |");
}
System.out.println(line);
System.out.println(divider);
while (rs.next()) {
line = new StringBuffer(blankline.toString());
line.setCharAt(0, '|');
for (int i = 0; i < numcols; i++) {
Object value = rs.getObject(i + 1);
if (value != null){
overwrite(line, colpos[i] + 1, value.toString().trim());
overwrite(line, colpos[i] + colwidths[i], " |");
}
}
System.out.println(line);
}
System.out.println(divider);
}
static void overwrite(StringBuffer b, int pos, String s) {
int len = s.length();
for (int i = 0; i < len; i++)
b.setCharAt(pos + i, s.charAt(i));
}
} |
Partager