我如何从文本字段中获取用户输入,将字符串传递给sql查询...... [英] how I can take an user input from text field that will pass string to sql query......

查看:122
本文介绍了我如何从文本字段中获取用户输入,将字符串传递给sql查询......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,我无法解决问题。我的代码是:



.......

hi i am new in this and i can not solve the problem.my code is:

.......

public void actionPerformed(ActionEvent e) {
		String nid = new String(t1.getText());
		String name = new String(t2.getText());
		String phn_no = new String(t3.getText());
		String email = new String(t4.getText());
		String address = new String(t5.getText());
		String room_no = new String(t6.getText());
		String iss_date = new String(t7.getText());
		String res_days = new String(t8.getText());
		String bill_paid = new String(t9.getText());
		String due = new String(t10.getText());
		if (e.getSource() == e1) {
			try{  
				//INSERTING DATA TO CUSTOMER TABLE
				//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("INSERT INTO CUSTOMER VALUES("+nid+","+name+","+phn_no+","+email+","+address+","+room_no+","+iss_date+","+res_days+","+bill_paid+","+due+ ")");


				while(rs.next()) 
					
				  
				  
				//step5 close the connection object  
				conn.close();  
				  
				}catch(Exception d){ System.out.println(e);}



................

..................

.....................

其实我要做的是我创造了一个有10个框架textfiedl从t1到t2,两个按钮保存并退出。

......


................
..................
.....................
actually what i am trying to do is that i have created a frame which have 10 textfiedl they are from t1 to t2 and two button "save and exit".
......

String nid = new String(t1.getText());
        String name = new String(t2.getText());
        String phn_no = new String(t3.getText());
        String email = new String(t4.getText());
        String address = new String(t5.getText());
        String room_no = new String(t6.getText());
        String iss_date = new String(t7.getText());
        String res_days = new String(t8.getText());
        String bill_paid = new String(t9.getText());
        String due = new String(t10.getText());



...............

...这部分将从文本字段中获取字符串值并将它们传递给sql查询


...............
...this part will take string value from text field and pass them to the sql query

ResultSet rs=stmt.executeQuery("INSERT INTO CUSTOMER VALUES("+nid+","+name+","+phn_no+","+email+","+address+","+room_no+","+iss_date+","+res_days+","+bill_paid+","+due+ ")");







如果我点击保存按钮然后执行查询,那么每个字符串都会被保存在database.please帮助我.......




if i click the button "save" and then the query will be executed and so every string will be saved in database.please help me.......

推荐答案

当你按下按钮时,你需要一个事件。

将读取文本框中的值,并且命令应该被触发到数据库。



请参考这里(我强烈建议给链接添加书签!)< br $>


https://docs.oracle.com /javase/tutorial/uiswing/index.html [ ^ ]



您的具体公关oblem在这里引用:



https ://docs.oracle.com/javase/tutorial/uiswing/events/index.html [ ^ ]
You need an Event, that is happening when you push the button.
The values from the textboxes will be read and the command should be triggered to the db.

Please refer here (I strongly recommend to bookmark the link!)

https://docs.oracle.com/javase/tutorial/uiswing/index.html[^]

your specific problem is referenced here:

https://docs.oracle.com/javase/tutorial/uiswing/events/index.html[^]


这篇关于我如何从文本字段中获取用户输入,将字符串传递给sql查询......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆