import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import java.awt.Dimension;
import java.sql.*;
import java.util.Enumeration;
import com.mysql.jdbc.Connection;
public class A extends JFrame {
public static final String DBDRVER ="org.gjt.mm.mysql.Driver";
//定义MySQL数据库的连接地址
public static final String DBURL="jdbc:mysql://localhost:3306/cpyd?characterEncoding=gbk";
//MySQL数据库的连接用户名
public static final String DBUSER="root";
//MySQL数据库的连接密码
public static final String DBPASS="123";
public static void main(String[] args) {
new A();
}
public A() {
JFrame frame=new JFrame();
Connection conn=null;
DefaultTableModel model;
JTable table;
try
{
Class.forName(DBDRVER);//加载驱动程序
conn=(Connection) DriverManager.getConnection(DBURL,DBUSER,DBPASS);//建立数据库连接
Statement st=conn.createStatement();
String sql="select * from user3";//假设要读的数据库表为user
ResultSet results=st.executeQuery(sql);
String[] biaotou = { "学号", "姓名", "性别",
"班级","密码", "出生年月","联系方式" };
Object [][]cellData={};
model=new DefaultTableModel(cellData,biaotou){ //表格用的数据模型
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;// 无条件返回 false,任何单元格都不让编辑。
}
};
table =new JTable(model);
String []data=new String [7];
while (results.next()) {
data[0] =results.getString(1);
data[1]=results.getString(2);
data[2]=results.getString(3);
data[3]=results.getString(4);
data[4]=results.getString(5);
data[5]=results.getString(6);
data[6]=results.getString(7);
model.addRow(data);
//tEditable(false);
}
// table.setModel(model);
results.close();
conn.close();
frame.getContentPane().setLayout(null);
table.setFillsViewportHeight(true);//填满容器高度
table.setPreferredScrollableViewportSize(new Dimension(500, 700));
JScrollPane scrollPane = new JScrollPane(table); //存放表格的,表格必须放在里面
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); //在所有的调整操作上成比例的调整所有的列
table.setDefaultRenderer(Object.class, new TableCellTextAreaRenderer());
scrollPane.setBounds(0, 0, 484, 461);
table.setRowHeight(30);
FitTableColumns(table); //宽度自适应方法
frame.getContentPane().add(scrollPane); //将srcollPane加入到frame中
frame.setTitle("欢迎进入学生查询界面");
frame.setSize(500,500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}catch(Exception e){
}
}
public static void FitTableColumns(JTable myTable) {
JTableHeader header = myTable.getTableHeader();
int rowCount = myTable.getRowCount();
Enumeration columns = myTable.getColumnModel().getColumns();
while (columns.hasMoreElements()) {
TableColumn column = (TableColumn) columns.nextElement();
int col = header.getColumnModel().getColumnIndex(column.getIdentifier());
int width = (int) myTable.getTableHeader().getDefaultRenderer()
.getTableCellRendererComponent(myTable, column.getIdentifier(), false, false, -1, col)
.getPreferredSize().getWidth();
for (int row = 0; row < rowCount; row++) {
int preferedWidth = (int) myTable.getCellRenderer(row, col)
.getTableCellRendererComponent(myTable, myTable.getValueAt(row, col), false, false, row, col)
.getPreferredSize().getWidth();
width = Math.max(width, preferedWidth);
}
header.setResizingColumn(column);
column.setWidth(width + myTable.getIntercellSpacing().width + 10);
}
}}
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.TableCellRenderer;
/*
*
* 表格设计自动换行设置
*/
public class TableCellTextAreaRenderer extends JTextArea implements TableCellRenderer {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public TableCellTextAreaRenderer() {
setLineWrap(true);
setWrapStyleWord(true);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
// 计算当下行的最佳高度
int maxPreferredHeight = 30;
for (int i = 0; i < table.getColumnCount(); i++) {
setText("" + table.getValueAt(row, i));
setSize(table.getColumnModel().getColumn(column).getWidth(), 0);
maxPreferredHeight = Math.max(maxPreferredHeight, getPreferredSize().height);
}
if (table.getRowHeight(row) != maxPreferredHeight){ // 少了这行则处理器瞎忙
table.setRowHeight(row, maxPreferredHeight); }
if(isSelected)
{
this.setBackground(table.getSelectionBackground());
}
else
{
this.setBackground(table.getBackground());
}
setText(value == null ? "" : value.toString());
return this;
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Test2 extends JFrame {
//从数据库中取出信息
//rowData用来存放行数据
//columnNames存放列名
Vector rowData,columnNames;
JTable jt=null;
JScrollPane jsp=null;
Connection ct=null;
ResultSet rs=null;
Statement ps=null;
public static void main(String[] args) {
Test2 test2=new Test2();
}
//构造函数
public Test2(){
columnNames=new Vector();
//设置列名
columnNames.add("学号");
columnNames.add("名字");
columnNames.add("性别");
columnNames.add("年龄");
columnNames.add("籍贯");
columnNames.add("系别");
columnNames.add("系别2");
rowData = new Vector();
//rowData可以存放多行,开始从数据库里取
try {
Class.forName("com.mysql.jdbc.Driver");
//(2)获取数据库连接
Connection ct=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/cpyd?useUnicode=true&characterEncoding=utf_8","root", "123");
//(3)创建SQL语句对象
Statement ps=ct.createStatement();
//(4)执行查询,返回结果集
ResultSet rs=ps.executeQuery("SELECT * FROM user3");
while(rs.next()){
//rowData可以存放多行
Vector hang=new Vector();
hang.add(rs.getString(1));
hang.add(rs.getString(2));
// hang.add(rs.getString(2).getBytes("lS08859-1"));
hang.add(rs.getString(3));
// hang.add(rs.getInt(4));
hang.add(rs.getString(4));
hang.add(rs.getString(5));
hang.add(rs.getString(6));
hang.add(rs.getString(7));
//加入到rowData
rowData.add(hang);
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(ct!=null){
ct.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//初始化Jtable
jt = new JTable(rowData,columnNames);
//初始化 jsp
jsp = new JScrollPane(jt);
//把jsp放入到jframe
this.add(jsp);
this.setSize(400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.TableCellRenderer;
/*
*
* 表格设计自动换行设置
*/
public class TableCellTextAreaRenderer extends JTextArea implements TableCellRenderer {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public TableCellTextAreaRenderer() {
setLineWrap(true);
setWrapStyleWord(true);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
// 计算当下行的最佳高度
int maxPreferredHeight = 30;
for (int i = 0; i < table.getColumnCount(); i++) {
setText("" + table.getValueAt(row, i));
setSize(table.getColumnModel().getColumn(column).getWidth(), 0);
maxPreferredHeight = Math.max(maxPreferredHeight, getPreferredSize().height);
}
if (table.getRowHeight(row) != maxPreferredHeight){ // 少了这行则处理器瞎忙
table.setRowHeight(row, maxPreferredHeight); }
if(isSelected)
{
this.setBackground(table.getSelectionBackground());
}
else
{
this.setBackground(table.getBackground());
}
setText(value == null ? "" : value.toString());
return this;
}
}