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
|
public class HibernateUtil {
public static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new AnnotationConfiguration().configure("hibernate.cfg.xml").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 {
// Step 1: Load the JDBC driver.
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver Loaded.");
// Step 2: Establish the connection to the database.
String url = "jdbc:mysql://localhost/base1";
conn = (Connection) DriverManager.getConnection(url, "root", "admin");
System.out.println("Got Connection.");
st = (Statement) conn.createStatement();
st.executeUpdate(sql);
} catch (Exception e) {
System.err.println("Got an exception! ");
e.printStackTrace();
System.exit(0);
}
}
public static void checkData(String sql) {
try {
HibernateUtil.outputResultSet((ResultSet) st
.executeQuery(sql));
// conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void outputResultSet(ResultSet rs) throws Exception{
ResultSetMetaData metadata = (ResultSetMetaData) 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);
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