2

I have a database with all the values stored in a table. I want to print a name from the table using drawString() method. I hv created a resultset and the arraylist. Is there any way to pass a string from database in drawString method??

Please provide me a code or link to a code as I am new to this..Thankyou.

public class AB1 extends JPanel
{   public static void main(String a[]) throws Exception
{
    int ID,TC;
    String FROM, TO, ATS, DTS, SOURCE, DSTN, TS, ST;
    ResultSet rs;

    ArrayList<AB> ab = new ArrayList<AB>();

    //DATABASE CONNECTION

    String TrainNo = "287972";
    try
    {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    String url = "jdbc:oracle:thin:@127.0.0.1:1521:xe";
    Connection c = DriverManager.getConnection(url, "system", "mypcdatabase");

    // STORING TABLE IN RESULTSET

    int rows = 0;

    PreparedStatement st=c.prepareStatement("select * from MT_TRAIN_CONSIST where TRAIN_NUMBER = ?" );
    rs=st.executeQuery();

    PreparedStatement st1=c.prepareStatement("select * from MT_RAKE_TRAIN_LINK_MASTER where TRAIN_NUMBER = ?");
    rs = st1.executeQuery();

    PreparedStatement st2=c.prepareStatement("select * from MT_SLIP_TRAIN_INFO  where TRAIN_NUMBER = ?");
    rs = st2.executeQuery();

    while(rs.next())
    {

    ID = rs.getInt("ID_TRAIN_DEF");
    FROM = rs.getString("COACH_FROM");
    TO = rs.getString("COACH_TO");
    TC = rs.getInt("NUMBER_OF_COACHES");
    ATS = rs.getString("ATTACH_TRANSFER_STATION");
    DTS = rs.getString("DETACH_TRANSFER_STATION");
    SOURCE = rs.getString("TRAIN_SRC");
    DSTN = rs.getString("TRAIN_DSTN");
    TS = rs.getString("TRANSFER_STATION");
    ST = rs.getString("slip_type");

    AB obj = new AB();

    obj.id = ID;
    obj.tc = TC;
    obj.from = FROM;
    obj.to = TO;
    obj.ats = ATS;
    obj.dts = DTS;
    obj.src = SOURCE;
    obj.dstn = DSTN;
    obj.ts = TS;
    obj.st = ST;
    ab.add(obj);

    rows++;
    }
}

catch(Exception ex)
{
    ex.printStackTrace();
}

getImg(new FileOutputStream("C:\\A&B" + TrainNo + ".jpg"), ab);
}

public static void getImg(OutputStream out, ArrayList<AB> ab) throws IOException
{    int imgWidth = 1024, imgHeight = 768;

BufferedImage image = new BufferedImage(imgWidth,imgHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D gx = image.createGraphics();
gx.setColor(Color.BLUE);

gx.setColor(Color.BLACK);
gx.drawLine(60,350,970,350);
gx.drawLine(970,350,960,340);
gx.drawLine(970, 350, 960, 360);
gx.drawString("", 30 , 330);
gx.drawString("", 960 , 330);

ImageIO.write(image, "PNG", out);
out.close();        
}
}       
Ankur Sharma
  • 53
  • 1
  • 6
  • 5
    *"I want to print a name from the table using drawString() method."* Better to display the string in a `JTextField` or `JLabel`. *"Is there any way to pass a string from database in drawString method??"* Yes, which part are you having trouble with? *"Please provide me a code"* SO is not a code factory. [What have you tried?](http://www.whathaveyoutried.com/) – Andrew Thompson Dec 09 '12 at 13:32
  • 1
    When you have your ResultSet and have extracted the String for your ResultSet, then why not just draw it? – OmniOwl Dec 09 '12 at 13:33
  • 1
    Won't this will do `g.drawString(rs.getString(column), 50, 50);` ? Or is it really tough :-) – nIcE cOw Dec 09 '12 at 13:47
  • 1
    Uhm, you have 99% of the code, not take that `ArrayList` in your paint method iterate through each `String` in `ArrayList` and use `Graphics#drawString(..)` unless I am missing something... – David Kroukamp Dec 09 '12 at 13:54
  • It is not picking values from database :( help me :( – Ankur Sharma Dec 09 '12 at 14:13
  • Since you seem to want an image, I suggest getting an image of a `JTable` as seen in [this question](http://stackoverflow.com/questions/7369814/why-does-the-jtable-header-not-appear-in-the-image). OTOH your latest comment indicates the DB access is not successful. Debug that first. – Andrew Thompson Dec 09 '12 at 14:19
  • @AnkurSharma : In your these lines **PreparedStatement st=c.prepareStatement("select * from MT_TRAIN_CONSIST where TRAIN_NUMBER = ?" ); rs=st.executeQuery();**, you forgot to provide any value for the **?**, something like `st.setString(1, TrainNo);` Please Learn [**Java Naming Conventions**](http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367) and stick to them. – nIcE cOw Dec 09 '12 at 14:23

2 Answers2

1

In your these lines

PreparedStatement st = c.prepareStatement("select * from MT_TRAIN_CONSIST where TRAIN_NUMBER = ?");
rs=st.executeQuery();

You forgot to provide any value for the ?, something like

PreparedStatement st = c.prepareStatement("select * from MT_TRAIN_CONSIST where TRAIN_NUMBER = ?");
st.setString(1, TrainNo);
rs=st.executeQuery();

Please Learn Java Naming Conventions and stick to them.

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
0

@ gagandeep bali the line is correct String TrainNo = "287972"; this value is passed in place of the ?

  • Do you mean automatically ? You have to provide it, as an argument to the `PreparedStatement` object, to make this work. – nIcE cOw Dec 09 '12 at 14:45