Java MySQL  ResultSet Help

How do I read the result of query -
"SELECT unix_timestamp(date_format(now(), '%Y-%m-%d %H') )"
from Java Result Set to a String variable in Mysql..
I tried -
Connection conn = getConnection( );
Statement s = connection.createStatement();
ResultSet rs = s.executeQuery( "SELECT unix_timestamp(date_format(now(), '%Y-%m-%d %H') )" );
String result = resultSet.getString( "unix_timestamp(date_format(now(), '%Y-%m-%d %H') )");
I don't get the value in result..rather I get the exception...My resultSet comes as not null from database.
Thanks!

You are using one variable when executing query and using another variable when accessing data. See the bolded values.
Connection conn = getConnection( );
Statement s = connection.createStatement();
ResultSet rs = s.executeQuery( "SELECT unix_timestamp(date_format(now(), '%Y-%m-%d %H') )" );
String result = resultSet.getString( "unix_timestamp(date_format(now(), '%Y-%m-%d %H') )");

Similar Messages

  • JDBC Java- mysql problem help!

    Hi, I wnat to connect to mySQL fromo Java, I followed all the instructions from the mySql web page, yet I havent been able to do it, when I compile the java progrm I keep getting class not found exceptions, I am using Fedora Core and Java was already installed-well I chose to install it when I installed the operating system, the point is that the paths are all assigned by he system not by me.
    WHat can I do?, Ive been trying over andover again with no results, hopefully somebody can help, I tried to include all the configuration in this email,
    thank you
    my java program is the following:
    t.java
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    public class t {
    public static void main (String[] args) {
    System.out.println("Hello, world!\n");
    Class.forName("com.mysql.jdbc.Driver");
    when I javac t.java
    I have the following error
    4. ERROR in t.java
    (at line 11)
    Class.forName("com.mysql.jdbc.Driver");
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    Unhandled exception type ClassNotFoundException
    when I comment out the Class.forName the hello world rogram runs fine
    when I vi /etc/java java.conf this is the result
    # System-wide Java configuration file -- sh --
    # JPackage Project [www.jpackage.org]
    # Location of jar files on the system
    JAVA_LIBDIR=/usr/share/java
    # Location of arch-specific jar files on the system
    JNI_LIBDIR=/usr/lib/java
    # Root of all JVM installations
    JVM_ROOT=/usr/lib/jvm
    # You can define a system-wide JVM root here if you're not using the default one#JAVA_HOME=$JVM_ROOT/java-gcj
    # Options to pass to the java interpreter
    JAVACMD_OPTS=
    ~
    [root@localhost test]# echo $PATH
    /usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
    echo $JAVA_HOME
    it contains nothing
    ls /usr/share java*
    java
    java-1.3.0
    java-1.4.0
    java-1.4.1
    java-1.4.2
    java-1.5.0
    javadoc
    java-ext
    java-utils
    mysql-connector-java-3.1.13-bin.jar is under
    /usr/share/java
    in /etc/profile I have:
    export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java-3.1.13-bin.jar
    in /root/.bash_profile i have
    CLASSPATH=/usr/share/java/mysql-connector-java-3.1.13-bin.jar:/usr/lib/java-ext/mysql-connector/mysql-connector-java-3.1.13-bin.jar
    export CLASSPATH
    when I echo $PATH i have
    /usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin

    Thanks duff,
    The driver seems to be found
    when I run
    public class t {
    public static void main (String[] args) {
    try
    System.out.println("Hello, world!\n");
    Class.forName("com.mysql.jdbc.Driver");
    System.out.println("MySQL Driver Found");
    catch (ClassNotFoundException e)
    System.out.println("MySQL Driver NOT Found");
    e.printStackTrace();
    I feel somehow ive been focusing in the wrong side of the problem due to my ignorance in Java. Please help me to understand, the reson for which ithe runtime was telling me that there was an unhandled exception was because it is a requirement to handle all exceptions of this kind? why wouldnt it cmplain if I jst did the hello world without any exception handling, maybe because it is a requirement in the Class.forName...
    the other question is, is the message I was obtaining completely independent of the fact that the class was or was not found?
    Maybe, I should go with these questions to the newbies forum...
    thanks

  • Java mysql ResultSet - how many rows

    Hi
    As a beginner java dude I'm trying to work with java on my mysql database instead of using perl (which works fine by the way). BTW this is not an apple specific question.
    I have properly managed to connect to my database and executa a query which returns a ResultSet object. By using the next() method I would like to populate an array with the results of my query using a while loop. All of this works fine. One problem persists however. I would like to know how many rows my query has so I can creat my array with the correct number , i.e if the query has 10 rows then the array should be String[] Array = new String[10].
    But i can only loop through the ResultSet once as it appears to be detsroyed after that. I have solved the issue by making two identical statements and I use one with getRow() in a while loop so I can create an array with the correct variables, and the other to loop through and populate the array.
    It seems to me there must be a better way to check how many rows a query returns ... i mean if I take the original query an put it through
    int nr = 0;
    while (rset.next()) {
    nr = rset.getRow();
    String[] Data = new String[nr];
    and then do
    while (rset.next()) {
    int count = rset.getRow();
    --count;
    int idnr = rset.getInt("One");
    String name = rset.getString("Name");
    Data[count] = idnr + " " + name;
    the the last while returns null results.
    How can withdraw the number of rows from the ResultSet and still use that same ResultSet in the last while loop? I have a feeling i'm missing something very elementary here.. but being new thats to be expected.
    Thanks

    Darn, just after I posted this I was struck by lightning and found an answer. The ResultSet isnt destroyed its just that the marker is at the end efter looping through a while loop, so if you want to loop again you have to the the marker to move to the absolute beginning.. thsi can be done using ResultSet.beforeFirst() which nice and neatly pops the marker back to its initial state.
    Sorry for bothering everyone.

  • Java mysql OK with Terminal but not OK with Apache local web server

    With terminal I have :
    CLASSPATH=/System/Library/Frameworks/JavaVM.framework/Versions/1.4.2/Classes/:/Library/Java/Extensions/:./
    SUDO_GID=20
    SECURITYSESSIONID=210800
    _=/usr/bin/printenv
    127:/Library/WebServer/Documents/Java root# java TestMySQL
    2006-05-21 13:39:00.0 ************************************************** = OK
    127:/Library/WebServer/Documents/Java root#
    import java.sql.*;
    * mysql-connector-java-3.1.12-bin.jar
    *   % javac /Library/WebServer/Documents/Java/TestMySQL.java
    *   % java TestMySQL
    public class TestMySQL {
            public static void main(String argv[]) throws Exception {
                    // Load the driver class
                    Class.forName("org.gjt.mm.mysql.Driver");
    1                //Class theClass = null;
    2               //try {
    3                //    theClass = Thread.currentThread().getContextClassLoader().loadClass("org.gjt.mm.mysql.Driver");
    4                //}
    5                //catch (ClassNotFoundException e) {
    6                //    theClass = getClass().getClassLoader().loadClass("org.gjt.mm.mysql.Driver");
    7                //}
                    Connection conn = DriverManager.getConnection(
                            "jdbc:mysql:///test",
                    Statement stmt = conn.createStatement();
                    ResultSet rset = stmt.executeQuery("SELECT now();");
                    while (rset.next()) {
                            System.out.println(rset.getString(1));
                    // Close result set, statement and DB connection
                    rset.close();
                    stmt.close();
                    conn.close();
    }I have a simple TestMySQL.html file with applet TestMySQL.class in it.
    Nothing but "Applet TestMySQL not inited"
    When I uncomment the try/catch lines (lines 1 to 7) , java compiler generate an error... near getClass().getClassLoader().loadClass("org.gjt.mm.mysql.Driver");
    With "Class.forName("org.gjt.mm.mysql.Driver");" no problem.
    Running MacOS X 10.3.9, Apache/1.3.33 (Darwin) PHP/4.4.1 mod_ssl/2.8.24 OpenSSL/0.9.7i and JVM 1.4.2_09
    Thanks for help.
    P.S. This my first approach to java/mysql but I would like to see this short program running on my local web server ...
    Thanks.

    hi :-)
    can you post the stack trace?
    im not sure what is causing the error because of less info,
    but have you tried to copy the driver of mysql to tomcat lib folder?
    regards,

  • How to do transactions in jsp pages using Java & MySQL ?

    Hi,
    I'm a newbie..
    I'd like to know "How to do transactions in jsp pages using Java & MySQL ?"
    Platform: Windows XP, Apache Tomcat 5.5, MySQL 5, Java bean without EJB
    what are the the different types of transactions? Differences between them?Pls provide examples?
    Which among them is the best method to implement a transaction?
    Pls help me...
    thnx in advance...

    http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html

  • Image size and mime type.. non-java guy needs help

    Image size, mime type.. non-java guy needs help
    Im not at all familiar with java so this is really weird for me to work out. I?ve been doing it all day (and half of yesterday).
    Im trying to write a custom clodFusion tag in java that gets the width, height, size and MIME types of a given file. I?ve been trying to get it to work on the command line first. I can get the width and height but cant get the size and the MIME type.
    Here is what I got
    /*import com.allaire.cfx.*;*/
    import java.awt.image.renderable.*;
    import javax.media.jai.*;
    import com.sun.media.jai.codec.*;
    import java.io.*;
    import java.util.*;
    public class ImageInfo {
    private RenderedOp image = null;
    private RenderedOp result = null;
    private int height = 0;
    private int width = 0;
    private String type = "";
    private String size = "";
    public void loadf(String file) throws IOException
    file = "80by80.jpg";
    FileSeekableStream fss = new FileSeekableStream(file);
    image = JAI.create("stream", fss);
    height = image.getHeight();
    width = image.getWidth();
    System.out.println(height + "\n");
    System.out.println(width);
    System.out.println(type);
    public static void main(String[] args) throws IOException {
    ImageInfo test = new ImageInfo();
    test.loadf(args[0]);
    can anyone please help me out to modify the above so I can also print the mime type and the file size to screen.
    thanks for any help

    any suggestions?

  • IMac: Install Java 7 need help!

    Hello, I've an iMac with the newest software version, I want to install java 7 on it for a game server, but when I download it from java.com and I install it and start the server, it still says: Computer on java 6, update to 7 to run server. I already tryed to install with Terminal and direct acess to System Library, it shows that java 7 is installed, but I have to delete java 6. When I delete all java versions, apple says I have to install java SE6, what isn't java 7 but java 6! Help please!!!

    The Oracle JRE is only a web plugin. If you need server-side Java, you'll have to use the Apple-supplied Java 6 runtime, or else the Oracle JDK (which is not a drop-in replacement for Apple's Java.) If your server application depends on Java 7, you'll most likely have to run it on another operating system.

  • I can't open ibooks. There was a screen pop up about java se. Help please.

    I can't open ibooks. There was a screen pop up about java se. Help please.

    Hi there ajsupprt,
    It sounds like you may need to update the version of Java installed on your computer. Take a look at the article below for more information.
    Java updates available for OS X on August 28, 2013
    http://support.apple.com/kb/ht5648
    -Griff W.

  • Oracle.jdbc.driver.oracleResultSet Vs java.sql.ResultSet

    Hi all
    Could any one got any idea on this problem...
    My application program is forced to use both Java Result Set(java.sql.ResultSet) and Oracle ResultSet(jdbc.driver.oracleResultSet).
    This on javac get compiled and is working fine....
    But when i try to compile with vbjc(visibroker-java compiler)... it failes to compile...
    throwing ...
    .."The method oracle.jdbc2.Blob getBlob(int) declared in class oracle.jdbc.driver.OracleResultSet cannot override the method of the same signature declared in interface java.sql.ResultSet. They must have the same return
    type."

    Hi Prowzen,
    Ur reply was much userful, which gave me a direction to prob into... Thank You Very MUCH.
    I relpaced
    BLOB blob = ((OracleResultSet)rs).getBLOB(int);
    with
    oracle.sql.BLOB blob =(BLOB)rs.getObject(int);
    So eliminating usage of OracleResultSet, the conflicts got cleared...
    Keep writing...
    Regards
    Sreekumar P P.
    [email protected]

  • Displaying Picture in a Java APPLICATION please help!!

    I have been trying to write a method that Displays a .jpg file when called. However I have not gotten far, every book I have tell you how to display pictures in an applet but not an application. I did find some code that is supposed to be for an application, but It does not compile right. Any help would be apprecidated!
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class PictureIt{
    public void makeImage() {
    //***Image Output Stream
    String imgFile
    = "d:\\temp\\TestImage.jpeg";
    out = new FileOutputStream(imgFile);
    BufferedImage image = null;
    image = (BufferedImage)createImage
    (panelJpeg.size.width, panelJpeg.size.height);
    Graphics g = image.getGraphics();
    panelJpeg.paintAll(g);
    }

    Displaying Picture in a Java APPLICATION please help!!
    Hope this helps.There is going to be two classes compile seperatly first class is what does the drawing
    here it is
    import javax.swing.*;
    import java.awt.*;
    public class draww extends JPanel {
    Image ball;
    int width1 = 100;
    int height1 = 100;
    public draww() {
    super();
    Toolkit kit = Toolkit.getDefaultToolkit();
    ball = kit.getImage("pic1.gif");
    public void paintComponent(Graphics comp) {
    Graphics2D comp2D = (Graphics2D) comp;
    comp2D.drawImage(ball, 20, 20, width1, height1, this);
    sound class is the container JFrame here it is
    import javax.swing.*;
    import java.awt.*;
    public class drawing extends JFrame {
    public drawing() {
    super("draw");
    setSize(400,400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container pane = getContentPane();
    draww d = new draww();
    pane.add(d);
    setContentPane(pane);
    setVisible(true);
    public static void main(String[] args) {
    drawing drawing1 = new drawing();
    PS Hope this helps and see you around

  • Java mysql connection

    Hi,
    Can anybody tell me how can I make the java-mysql JDBC connection using IDS Server ( type 3 driver )
    this is the only driver which bypass proxy/firewall
    if any one use this driver before pls give me more information about it.

    Well, first you have to obtain a type 3 driver for MySQL. I don't know if such a driver exists.
    Assuming one does, you'd need a properly configured server.
    Once you've got that, the Java code should be identical to that for any other driver.

  • Can i download java for my ipad, the problem is i open glovis.usgs from my ipad, but i can't see the full page, cause the page need a java...help me please

    Can i download java for my ipad, the problem is i open glovis.usgs from my ipad, but i can't see the full page, cause the page need a java...help me please

    Nope. The iPad (and iPhone and iPod touch) cannot execute Java applets. It's a limitation in iOS from the beginning (just like lack of Flash). You will either need to use a desktop computer or access a desktop computer via RDP or VNC to access that page on iPad.mIn short: there is no native way to use a Java applet on iOS.

  • Problems implementing java.sql.ResultSet on AbstractTableModel

    Hi java Gurus !!
    I have a trouble implementing interface java.sql.ResultSet on javax.swing.table.AbstractTableModel. The AbstractTableModel class defines a method like
    this:
    int findColumn(String column)
    while the java.sql.Resultset defines a method as follows:
    int findColumn(String column) throws SQLException.
    the compiler gives me the following error:
    "Error #: 462 : method findColumn(java.lang.String) in class inducontrol.Untitled1 cannot override method findColumn(java.lang.String) in class javax.swing.table.AbstractTableModel, overridden method does not throw java.sql.SQLException at line 213, column 14
    How can I solve it?
    thanks a lot !!!!

    Hassen is correct, it seems that you mix two different things.
    Can you show code excerpts so we can give you some tips ?

  • I need to learn how to use all about java & mysql...help me!

    I have a situation here, I need to learn how to use java with mysql
    . Can I connect to a MYSQL DB with servlets?
    how can I build an e-mail server with java (no matter how difficult)
    please, I need help, and I really apreciate your help.
    thank you very much!!

    I have a situation here, I need to learn how to use
    java with mysql
    . Can I connect to a MYSQL DB with servlets?Yes... documentation to help you connect to any database can be found at http://java.sun.com/products/jdbc. To connect to MySQL, you'll need drivers (sourceforge.net), and the specific connection URL for those drivers will be included in the documentation.
    how can I build an e-mail server with java (no matter
    how difficult)If you're fairly new to JSP/Servlets, you may be in over your head here, since an email server is no easy application to code. Here's a link to the source code for the JAMES project... Apache's Java email server... maybe you can find some useful information there...
    http://www.ibiblio.org/pub/packages/infosystems/WWW/servers/apache/jakarta/james/source/

  • JAVA + MYSQL + ACCESS + ENCODING!!! help needed here!!!!

    i have to migrate some data from access to mysql. however, i have some japanese characters in the access table and whenever i am done w/ migration and open the mysql table, i see bunch of ?????? instead japanese character. since im now working for a japanese company, this pc is in japanese eviorioment.
    i set mysql table to use utf8. does anyone here know whats going wrong? i guess there is something to do w/ encoding stuff. please help!!!
    here is some more information:
    java1.5.1, access2003, mysql5.1 and japanese xp

    This is the third thread you have opened on the same problem within the last couple of days, and you have not provided any more information than in the first one that would let people figure out what you are doing and thus help you.
    In the first thread you opened it seems you had gotten to the point of realizing that you couldn't insert Japanese characters correctly in your mysql database, right? So it seems that the problem is simply with your mysql configuration and/or the way you interact with it from Java.
    So, to narrow the problem down further:
    1. Can you insert Japanese directly into mysql (not from Java) and get them rendered correctly? That would indicate your DB is configured correctly.
    2. What is the code you use to insert Japanese characters into mysql? (start with hardcoded Japanese, don't mix the Access migration into this yet)

Maybe you are looking for

  • Actionscript help: can not have more than one externally visable def

    package  {     // these are flash built in classes      import flash.display.MovieClip;      import flash.events.TimerEvent;     import flash.utils.Timer;      import flash.events.Event;     //Our own custom class     import MainTimer;     import fla

  • Problem With Tv Shows Menu

    i just recently downloaded the new itunes software. and was susrprised to see that you could now select TV shows to sort out your video library( finaly itunes got the message) and quickyly changed all my collection of TV shows to the right sort. but

  • Solid state hard drive

    i am trying to replace a failing hard drive in my HP G72 notebook.  I am trying to replace the 320 Gig hard drive with a 250 Gig solid state.  i have run the restore disks and everything appears to be ok but when it restarts i still get a non boot de

  • Self Appraisal Template

    Hi I have created a template for self appraisal.  Created a document using this template This document expected to be in ESS view and not in MSS view. Unfortunately am able to see in both views in portal. Help need where am i missing the setup . Joe

  • I want to play podcast episodes just once

    I recently bought an ipod shuffle 4th generation to listen to podcasts but i've noticed that when it finishes playing all the episodes of a given podcast in continuous order it goes back to the first episode and begins to play it again even thought i