Inserir conteudo de um jtextfield no banco

2 respostas
D

oi…de novo eu…comecando com java…como faco pra pegar o conteudo de um jtextfield e lancar no banco de dados…alguem poderia disponibilizar em exemplo simples…

obrigado. :oops:

2 Respostas

C

Ola!

Você ja sabe JDBC? Espero que sim :slight_smile: Se não, procure um outro fórum sobre esse assunto, porque já falaram muito sobre isso.

Para pegar o conteúdo de um JTextField e inserir no banco, é só concatenar o texto digitado nele na string SQL que você vai executar. Por exemplo,



Connection conexao = ;

String sql = INSERT INTO tabela (campo1, campo2, campo3) VALUES (?, ?, ?);

PreparedStatement instrucao = conexao.prepareStatement(sql);

instrucao.setString(1, seu_textfield.getText());

instrucao.setString(2, );

instrucao.setString(3, );

instrucao.executeUpdate();



conexao.close();


Se o campo do banco que você vai inserir não for varchar, substitua o setString por setInt, setBoolean… ou qualquer outro tipo que você for usar.

[]'s!

C

Toma aí.
Tá integrado com ACCESS.
Abraço.
Charles

package TabelasB;


import com.cloudgarden.layout.AnchorLayout;
import javax.swing.JTextField;
import com.cloudgarden.layout.AnchorConstraint;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.ActionEvent;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;





/**
* This code was generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* *************************************
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED
* for this machine, so Jigloo or this code cannot be used legally
* for any corporate or commercial purpose.
* *************************************
*/
public class Biblioteca extends javax.swing.JPanel {
	private JLabel lblBiblLogo;
	private JLabel lblNomedaObra;
	private JLabel lblTipoMat;
	private JLabel lblLocalDeaArmazenamento;
	private JTextField tipoMaterial;
	private JTextField nomedaobra;
	private JTextField autor;
	private JLabel lblAutor;
	private JTextField localdeArmazenamento;
	private JTextArea jTextArea1;
	private JButton jButton1;
	private JTextField editora;
	private JLabel lblEditora;
	private JTextField status;
	private JLabel lblStatus;
	private JLabel lblBiblTit;

	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	
	
	
	public Biblioteca() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			AnchorLayout thisLayout = new AnchorLayout();
			this.setLayout(thisLayout);
			this.setPreferredSize(new java.awt.Dimension(450, 391));
			{
				tipoMaterial = new JTextField();
				this.add(tipoMaterial, new AnchorConstraint(
					369,
					963,
					413,
					258,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL));
				tipoMaterial.setPreferredSize(new java.awt.Dimension(317, 17));
			}
			{
				jTextArea1 = new JTextArea();
				this.add(jTextArea1, new AnchorConstraint(
					776,
					836,
					950,
					365,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL));
				jTextArea1.setPreferredSize(new java.awt.Dimension(212, 68));
			}
			{
				jButton1 = new JButton();
				this.add(jButton1, new AnchorConstraint(
					789,
					276,
					865,
					32,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL));
				jButton1.setText("enviar dados");
				jButton1.setPreferredSize(new java.awt.Dimension(110, 30));
				jButton1.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						System.out.println("jButton1.actionPerformed, event="
							+ evt);
						//TODO add your code for jButton1.actionPerformed
						
					
						String t=nomedaobra.getText();
						String x=tipoMaterial.getText();
						String y=status.getText();
						String z=editora.getText();
						String a=localdeArmazenamento.getText();
						String b=autor.getText();
						try {
							jTextArea1.append(InsereBD(t,x,y,z,a,b));
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
							
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
				});
			}
			
			{
				autor = new JTextField();
				this.add(autor, new AnchorConstraint(
					684,
					963,
					727,
					258,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL));
				autor.setPreferredSize(new java.awt.Dimension(317, 17));
			}
			{
				nomedaobra = new JTextField();
				this.add(nomedaobra, new AnchorConstraint(
					297,
					963,
					338,
					258,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL));
				nomedaobra.setPreferredSize(new java.awt.Dimension(317, 16));
			}
			{
				lblAutor = new JLabel();
				this.add(lblAutor, new AnchorConstraint(
					671,
					125,
					755,
					25,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL));
				lblAutor.setText("Autor:");
				lblAutor.setPreferredSize(new java.awt.Dimension(45, 33));
			}
			{
				localdeArmazenamento = new JTextField();
				this.add(localdeArmazenamento, new AnchorConstraint(
					599,
					963,
					643,
					256,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL));
				localdeArmazenamento.setPreferredSize(new java.awt.Dimension(318, 17));
				localdeArmazenamento.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						localdeArmazenamentoActionPerformed(evt);
					}
				});
			}
			{
				lblLocalDeaArmazenamento = new JLabel();
				this.add(lblLocalDeaArmazenamento, new AnchorConstraint(
					579,
					221,
					679,
					27,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL));
				lblLocalDeaArmazenamento.setText("Local:");
				lblLocalDeaArmazenamento.setPreferredSize(new java.awt.Dimension(87, 39));
			}
			{
				editora = new JTextField();
				this.add(editora, new AnchorConstraint(
					520,
					961,
					563,
					256,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL));
				editora.setPreferredSize(new java.awt.Dimension(317, 17));
			}
			{
				lblEditora = new JLabel();
				this.add(lblEditora, new AnchorConstraint(
					500,
					158,
					599,
					25,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL));
				lblEditora.setText("Editora:");
				lblEditora.setPreferredSize(new java.awt.Dimension(60, 39));
			}
			{
				status = new JTextField();
				this.add(status, new AnchorConstraint(
					443,
					963,
					487,
					258,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL));
				status.setPreferredSize(new java.awt.Dimension(317, 17));
			}
			{
				lblStatus = new JLabel();
				this.add(lblStatus, new AnchorConstraint(
					420,
					161,
					520,
					27,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL));
				lblStatus.setText("Status:");
				lblStatus.setPreferredSize(new java.awt.Dimension(60, 39));
			}
			{
				lblTipoMat = new JLabel();
				this.add(lblTipoMat, new AnchorConstraint(
					351,
					214,
					451,
					25,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL));
				lblTipoMat.setText("Tipo Material:");
				lblTipoMat.setPreferredSize(new java.awt.Dimension(85, 39));
			}
			{
				lblNomedaObra = new JLabel();
				AnchorLayout jLabel1Layout = new AnchorLayout();
				this.add(lblNomedaObra, new AnchorConstraint(
					280,
					238,
					356,
					25,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL));
				lblNomedaObra.setText("Nome da Obra:");
				lblNomedaObra.setPreferredSize(new java.awt.Dimension(96, 30));
				lblNomedaObra.setLayout(null);
			}
			{
				lblBiblTit = new JLabel();
				this.add(lblBiblTit, new AnchorConstraint(
					75,
					754,
					175,
					416,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL));
				lblBiblTit.setText("Criar Biblioteca");
				lblBiblTit.setPreferredSize(new java.awt.Dimension(152, 39));
				lblBiblTit.setFont(new java.awt.Font("Dialog",1,16));
			}
			{
				lblBiblLogo = new JLabel();
				AnchorLayout lblBiblLogoLayout = new AnchorLayout();
				lblBiblLogo.setLayout(null);
				this.add(lblBiblLogo, new AnchorConstraint(
					49,
					203,
					216,
					18,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL,
					AnchorConstraint.ANCHOR_REL));
				lblBiblLogo.setIcon(new ImageIcon(getClass().getClassLoader().getResource("Imagem/Logo GroupOffice.jpg")));
				lblBiblLogo.setPreferredSize(new java.awt.Dimension(83, 65));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @param w
	 * @param x
	 * @param y
	 * @param z
	 * @param a
	 * @param b
	 * @return
	 */

	

	/**
	* Auto-generated method for setting the popup menu for a component
	*/
	private void setComponentPopupMenu(final java.awt.Component parent, final javax.swing.JPopupMenu menu) {
parent.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent e) {
if(e.isPopupTrigger())
menu.show(parent, e.getX(), e.getY());
}
public void mouseReleased(java.awt.event.MouseEvent e) {
if(e.isPopupTrigger())
menu.show(parent, e.getX(), e.getY());
}
});
	}
	
	private void autorActionPerformed(ActionEvent evt) {
System.out.println("autor.actionPerformed, event="+evt);
//TODO add your code for autor.actionPerformed
	}
	
	private void localdeArmazenamentoActionPerformed(ActionEvent evt) {
System.out.println("localdeArmazenamento.actionPerformed, event="+evt);
//TODO add your code for localdeArmazenamento.actionPerformed
	}
	
	private void editoraActionPerformed(ActionEvent evt) {
System.out.println("editora.actionPerformed, event="+evt);
//TODO add your code for editora.actionPerformed
	}
	
	private void statusActionPerformed(ActionEvent evt) {
System.out.println("status.actionPerformed, event="+evt);
//TODO add your code for status.actionPerformed
	}
	
	private void tipoMaterialActionPerformed(ActionEvent evt) {
System.out.println("tipoMaterial.actionPerformed, event="+evt);
//TODO add your code for tipoMaterial.actionPerformed
	}
	
	private void idObraActionPerformed(ActionEvent evt) {
System.out.println("idObra.actionPerformed, event="+evt);
//TODO add your code for idObra.actionPerformed
	}
	
	String InsereBD(String t, String x, String y, String z,String a,String b)throws SQLException, IOException{	
		
		String resp="";
		
		String nome,tipomaterial,status,editora,localdearmazenamento,autor;

	    resp+="Carregando o driver JDBC ODBC...";
	    try
	    {
	        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	    }
	    catch(ClassNotFoundException Erro)
	    {
	    	resp+="\nNão e possivel carregar o driver...";
	        Erro.printStackTrace();
	        return resp;
	    }

	    resp+="\nConectando-se à Base de Dados";
	    Connection Conexao = DriverManager.getConnection("jdbc:odbc:BancodeDados","","");
	    Statement FluxoSQL = Conexao.createStatement();
	    
	    
	    
	    nome=t;
	    tipomaterial = x;
	    status = y; // Modificar para float
	    editora = z;
	    localdearmazenamento=a;
		autor=b;
		

	    String InstrucaoSQL  = "INSERT INTO obra VALUES ('"+nome+"','"+tipomaterial+ "','" +status+ "','" +editora+ 
	    "','"+localdearmazenamento+"','"+autor+"')"; 
	    

	    FluxoSQL.executeUpdate(InstrucaoSQL);
	    resp+="\nInstrução SQL executada com sucesso...\n\n";
	    FluxoSQL.close();
	    Conexao.close();
	    return resp;
	}

	

	

	}
Criado 27 de junho de 2005
Ultima resposta 28 de jun. de 2005
Respostas 2
Participantes 3