Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can not run the code please help me to solve it.Actually I am trying to take the string input in the code from the text field and place it on the sql query like this:

Java
ResultSet rs=stmt.executeQuery("select ' "+cid+" ' from data");


which will show me the information from the database but it's not working.When I am writing the code like this:

Java
ResultSet rs=stmt.executeQuery("select * from data");   

it is working.
Can you please help me.The full code is attached.

.......................................
Java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


class nishan implements ActionListener{

	JFrame f1=new JFrame("New");
	
	JLabel l1=new JLabel("column");
	
	
		JButton b1=new JButton("Done");
		
		JTextField t1=new JTextField();
	
		
	nishan(){
		
               
		b1.setBounds(10, 170,90, 70);
	
		
		l1.setBounds(10,20,80,80);
		
		
		t1.setBounds(60,50,150,20);
		
		
		
		f1.add(b1);
	
		f1.add(l1);
		
		f1.add(t1);
	

		b1.addActionListener(this);
		
		

		f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f1.setLayout(null);
		f1.setVisible(true);
		f1.setLocationRelativeTo(null);
		f1.setSize(500, 370);
                //f1.setBackground(Color.ORANGE);

               
	}	
		
	public void actionPerformed(ActionEvent e) {
		String cid = new String(t1.getText());
		if(e.getSource()==b1){
			
			try{  
				
				//step1 load the driver class  
				Class.forName("oracle.jdbc.driver.OracleDriver");  
				  
				//step2 create  the connection object  
				Connection conn=DriverManager.getConnection(  
				"jdbc:oracle:thin:@localhost:1521:xe","system","almaksud");  
				  
				//step3 create the statement object  
				Statement stmt=conn.createStatement();  
			
				//step4 execute query  
				ResultSet rs=stmt.executeQuery("select '"+cid+"' from data");
				while(rs.next()) 
					
				System.out.println(rs.getString("name")+"  "+rs.getInt("id"));  
				  
				//step5 close the connection object  
				conn.close();  
				  
				}catch(Exception d){ System.out.println(e);}
		}
		
	}
	
	
	
}
public class justdo {

	public static void main(String[] args) {
		
                        new nishan();
		
	}

}
Posted
Updated 13-Apr-15 20:58pm
v3

1 solution

ResultSet rs=stmt.executeQuery("select ' "+cid+" ' from data");
You don't need the quotes in this line.
Quotes would return the cid value as is.
Try ResultSet rs=stmt.executeQuery("select "+cid+" from data");

As a note, there are some security risks associated with this approach - Understanding SQL Injection and Creating SQL Injection Proof ASP.NET Applications[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900