RSS
Təsvir

1.Java Swing JTable doldurmaq MySql ilə

27 Noy

1.ctreate table in mysql

create table invertar.Example(
id int auto_increment Primary Key,
name varchar(30),
birthday Date,
status varchar(1)  //sonra bize lazimolar..
)

  1. java  example classimizi yaradırıq.

package az;

import java.util.Date;

/**
*
* @author AqilI
*/

public class Example {
private int id;
private String name;
private Date birthday;
private String status;

public Example() {
}

public Example(int id, String name, Date birthday,String status) {
this.id = id;
this.name = name;
this.birthday = birthday;
this.status=status;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Date getBirthday() {
return birthday;
}

public void setBirthday(Date birthday) {
this.birthday = birthday;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

}

3.Connection classımızı yaradaq.    // proyektə  mysq jar file əlavə etməyi unutmayin))

import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import logn.Login;

/**
*
* @author AqilI
*/
public class Connection {

private static java.sql.Connection conn = null;
private static PreparedStatement psate = null;
private static ResultSet rs = null;
private static final String user = “root”;
private static final String password = “sizin şifrə“;
private static final String url = “mysql url“;

public static void connection() throws SQLException {
try {
Class.forName(“com.mysql.jdbc.Driver”);
conn = DriverManager.getConnection(url, user, password);

} catch (ClassNotFoundException e) {
System.out.println(“Driver tapilmadi: ” + ” ” + e.getMessage());
e.printStackTrace();
}
}

public static void closed() {
try {
if (conn != null) {
conn.close();
}
if (psate != null) {
psate.close();
}
} catch (Exception e) {
System.out.println(“Connection Baglanmadi! ” + e.getMessage());
e.printStackTrace();
}
}

//listlə doldururuq

public List<Example> allExample() throws Exception {
try {
connection();
List<Example> exmple = new ArrayList<>();
String sql = “select * from invertar.example“;
psate = conn.prepareStatement(sql);
rs = psate.executeQuery();
while (rs.next()) {
Example e = new Example();
e.setId(rs.getInt(1));
e.setName(rs.getString(2));
e.setBirthday(rs.getDate(3));

exmple .add(e);

}
return exmple ;
} catch (SQLException sql) {
System.out.println(“Xeta: ” + sql.getMessage());
sql.printStackTrace();
return null;
} finally {
closed();
}
}

//table header

public List<String> headerExample() {
List<String> head = new ArrayList<String>();
head.add(“Id”);
head.add(“Name”);
head.add(“Birthday”);
return head;
}

}

4.Table model classimizi yaradaq.

package az;

import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
*
* @author AqilI
*/
public class ExampleTableModel extends AbstractTableModel {

private List<Example> list;
private List<String> head;

public ExampleTableModel(List<Example> list, List<String> head) {
this.list = list;
this.head = head;
}

@Override
public String getColumnName(int column) {
return head.get(column);
}

@Override
public int getRowCount() {
return list.size();
}

@Override
public int getColumnCount() {
return head.size();
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
if (columnIndex == 0) {
return list.get(rowIndex).getId();
} else if (columnIndex == 1) {
return list.get(rowIndex).getName();
} else {
return list.get(rowIndex).getBirthday();
}
}
}

  1. jframe Table form

package az;

import java.awt.Color;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ListSelectionModel;

/**
*
* @author AqilI
*/
public class ExampleTable extends javax.swing.JFrame {

final Connection con = new Connection();
final Example e = new Example();

public ExampleTable() {
try {
initComponents();
setLocationRelativeTo(this);
setTitle(“Example Table from“);
refleshTbModel();
tbExample.setShowGrid(true);
tbExample.setSelectionForeground(Color.RED);
tbExample.setSelectionBackground(Color.LIGHT_GRAY);
tbExample.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tbExample.setRowHeight(20);
tbExample.setRowSelectionAllowed(true);
this.pack();

} catch (Exception ex) {
Logger.getLogger(ExampleTable.class.getName()).log(Level.SEVERE, null, ex);
}
}

public void refleshTbModel() throws Exception {
ExampleTableModel table_Model = new ExampleTableModel(con.allExample(), con.headerExample());
tbExample.setModel(table_Model);

}

@SuppressWarnings(“unchecked”)
// <editor-fold defaultstate=”collapsed” desc=”Generated Code”>
private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();
tbExample = new javax.swing.JTable();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jScrollPane1.setViewportView(tbExample);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 635, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(14, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);

pack();
}// </editor-fold>

/**
* @param args the command line arguments
/
public static void main(String args[]) {
/
Set the Nimbus look and feel /
//<editor-fold defaultstate=”collapsed” desc=” Look and feel setting code (optional) “>
/
If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if (“Nimbus”.equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ExampleTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ExampleTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ExampleTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ExampleTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ExampleTable().setVisible(true);
}
});
}

// Variables declaration – do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tbExample;
// End of variables declaration
}

 

table

\ davami olaraq insert, delete, update ,search və jogin page hissələrini paylaşmağacçalışaram.

Advertisements
 
Bir şərh yazın

Müəllif Açıq 27 Noyabr 2016 in SWİNG

 

Bir şərh yazın

Sistemə daxil olmaq üçün məlumatlarınızı daxil edin və ya ikonlardan birinə tıklayın:

WordPress.com Loqosu

WordPress.com hesabınızdan istifadə edərək şərh edirsinz. Çıxış / Dəyişdir )

Twitter rəsmi

Twitter hesabınızdan istifadə edərək şərh edirsinz. Çıxış / Dəyişdir )

Facebook fotosu

Facebook hesabınızdan istifadə edərək şərh edirsinz. Çıxış / Dəyişdir )

Google+ foto

Google+ hesabınızdan istifadə edərək şərh edirsinz. Çıxış / Dəyişdir )

%s qoşulma

 
%d bloqqer bunu bəyənir: