Jump to content
BulForum.com

Java


ZGAN

Recommended Posts

Tova e za edna poznata ako moje nqkoi da help

 

package jdbcdemo;

import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import javax.swing.table.*;
import java.util.*;
import java.sql.*;
import java.awt.event.*;


public class Frame1 extends JFrame {
 BorderLayout borderLayout1 = new BorderLayout();
 JPanel jPanel1 = new JPanel();
 BorderLayout borderLayout2 = new BorderLayout();
 Border border1;
 JPanel jPanel2 = new JPanel();
 JPanel jPanel3 = new JPanel();
 Border border2;
 JLabel jLabel1 = new JLabel();
 JButton jButton1 = new JButton();
 JButton jButton2 = new JButton();
 Border border3;
 BorderLayout borderLayout3 = new BorderLayout();
 JScrollPane jScrollPane1 = new JScrollPane();
 JTable jTable1;//= new JTable();
 DefaultTableModel defaultTableModel1 = new DefaultTableModel();

 String name = "";
 String url = "jdbc:odbc:library";
 String user = "sa";
 String password = "";
 Connection conn;
 Statement stmt;

 public Frame1() {
try {
  jbInit();
}
catch(Exception ex) {
  ex.printStackTrace();
}
 }

 void jbInit() throws Exception {
border1 = new EtchedBorder(EtchedBorder.RAISED,Color.white,new Color(165, 163, 151));
border2 = new EtchedBorder(EtchedBorder.RAISED,Color.white,new Color(165, 163, 151));
border3 = new EtchedBorder(EtchedBorder.RAISED,Color.white,new Color(165, 163, 151));
this.getContentPane().setLayout(borderLayout1);
jPanel1.setLayout(borderLayout2);
jPanel1.setBorder(border1);
jPanel3.setBorder(border2);
jLabel1.setText("izbor:");
jButton1.setText("Vavegdane");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
jButton2.setText("Iztrivane");
jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));
jPanel2.setBorder(border3);
jPanel2.setDebugGraphicsOptions(0);
jPanel2.setLayout(borderLayout3);
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jPanel2, BorderLayout.CENTER);
jPanel2.add(jScrollPane1, BorderLayout.CENTER);

defaultTableModel1.addColumn("ID");
defaultTableModel1.addColumn("Фамилия");
defaultTableModel1.addColumn("Име");
defaultTableModel1.addColumn("Телефони");
defaultTableModel1.addColumn("Адрес");

try
{

  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

  conn = DriverManager.getConnection(url, user, password);
  stmt = conn.createStatement();

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

  while (rs.next()) {
	Vector temp = new Vector();
	temp.add(rs.getObject(1));
	temp.add(rs.getObject(2));
	temp.add(rs.getObject(3));
	temp.add(rs.getObject(4));
	temp.add(rs.getObject(5));
	defaultTableModel1.addRow(temp);
  }
  rs.close();
  stmt.close();
}
catch (ClassNotFoundException ex) {
  System.out.println("Exception: " + ex.toString());
}
catch (SQLException ex) {
  System.out.println("Exception: " + ex.toString());
}


jTable1= new JTable(defaultTableModel1);



jScrollPane1.getViewport().add(jTable1, null);
jPanel1.add(jPanel3,  BorderLayout.SOUTH);
jPanel3.add(jLabel1, null);
jPanel3.add(jButton1, null);
jPanel3.add(jButton2, null);
 }

 public static void main(String[] args) {
Frame1 frame1 = new Frame1();
frame1.setSize(500,450);
frame1.setTitle("Таблица с клиенти");
frame1.setVisible(true);
 }

 void jButton2_actionPerformed(ActionEvent e) {
int[] sel_rows = jTable1.getSelectedRows();

try {
  PreparedStatement ps = conn.prepareStatement("DELETE FROM CLIENT WHERE CLIENT_ID=?");
  for (int a = (sel_rows.length - 1); a >= 0; a--) {
	String id_str=defaultTableModel1.getValueAt(sel_rows[a],0).toString();
	int id_int=Integer.parseInt(id_str);
	ps.setInt(1,id_int);
	ps.execute();
	defaultTableModel1.removeRow(sel_rows[a]);
  }
}
catch (java.sql.SQLException sqle) {
  sqle.printStackTrace();
}

 }

 void jButton1_actionPerformed(ActionEvent e) {

 }

}

class Frame1_jButton2_actionAdapter implements java.awt.event.ActionListener {
 Frame1 adaptee;

 Frame1_jButton2_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
 }
 public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
 }
}

class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener {
 Frame1 adaptee;

 Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
 }
 public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
 }
}

 

Това е кода който съм написала – в него е всичко: създаването на форма в Java, връзките с таблиците които отделно съм създала в SQL и изтриване на данни в таблиците.

Сега ми трябва само код на Java (допълнение на този) , чрез който да може да се добавят данни в таблицата която е в SQL .

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...