java - getDate from mysql -
how can convert date mysql database variable in java? have tried far:
try { connection connection = drivermanager.getconnection(database, user, pass); statement statement = connection.createstatement(); string sql = "select mhs.angkatan t_nilai nilai join t_mahasiswa mhs on nilai.nim = mhs.nim mhs.nim = '" + txt_nim + "'"; resultset resultset = statement.executequery(sql); date angkatan = resultset.getdate("angkatan"); resultset.close(); statement.close(); connection.close(); } catch (exception e) { system.err.println(e.getmessage()); joptionpane.showmessagedialog(null, e.getmessage(), "error", joptionpane.information_message); system.exit(0); }
check this posting out please. there example there
___update___
(below code taken link gave above. balkrishna rawool heads up)
import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; public class main { public static connection getmysqlconnection() throws exception { string driver = "org.gjt.mm.mysql.driver"; string url = "jdbc:mysql://localhost/databasename"; string username = "root"; string password = "root"; class.forname(driver); return drivermanager.getconnection(url, username, password); } public static void main(string args[]) { resultset rs = null; connection conn = null; statement stmt = null; try { conn = getmysqlconnection(); stmt = conn.createstatement(); rs = stmt.executequery("select timecol, datecol, datetimecol datetimetable"); while (rs.next()) { java.sql.time dbsqltime = rs.gettime(1); java.sql.date dbsqldate = rs.getdate(2); java.sql.timestamp dbsqltimestamp = rs.gettimestamp(3); system.out.println("dbsqltime=" + dbsqltime); system.out.println("dbsqldate=" + dbsqldate); system.out.println("dbsqltimestamp=" + dbsqltimestamp); java.util.date dbsqltimeconverted = new java.util.date(dbsqltime.gettime()); java.util.date dbsqldateconverted = new java.util.date(dbsqldate.gettime()); system.out.println("in standard date"); system.out.println(dbsqltimeconverted); system.out.println(dbsqldateconverted); } } catch (exception e) { e.printstacktrace(); } { try { rs.close(); stmt.close(); conn.close(); } catch (sqlexception e) { e.printstacktrace(); } } } }
Comments
Post a Comment