如何利用java输出一个数据库表的内容?
展开全部
给你个示列
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Stmessege1 extends JFrame {
private static final long serialVersionUID = 1L;
private JTable table;// 声明表格
void Stmessege11() {
setSize(460, 320);
setVisible(true);
}
public Vector getDataVector() throws SQLException, ClassNotFoundException {
Connection conn = null;
java.sql.Statement stmt = null;
Class.forName("com.mysql.jdbc.Driver");
String dburl = "jdbc:mysql://localhost:3306/hp";
String dbuser = "root";
String dbpassword = "19920102";
conn = DriverManager.getConnection(dburl, dbuser, dbpassword);
stmt = conn.createStatement();
String sql = "select* from xsgli";
ResultSet rs = stmt.executeQuery(sql);
Vector dataVector = new Vector();
while (rs.next()) {
Vector rowVector = new Vector();
for (int i = 1; i <= 7; i++) {
if (i == 2 || i == 4) {
rowVector.add(String.valueOf(rs.getInt(i)));
} else {
rowVector.add(rs.getString(i));
}
}
dataVector.add(rowVector);
}
return dataVector;
}
public Vector getColumnVector() {
Vector columnVector = new Vector();
columnVector.add("姓名");
columnVector.add("学号");
columnVector.add("性别");
columnVector.add("班级");
columnVector.add("系别");
columnVector.add("成绩");
columnVector.add("专业");
return columnVector;
}
public Stmessege1() throws SQLException, ClassNotFoundException {
super();
setTitle("学生学籍数据显示");
setBounds(100, 100, 382, 223);
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Connection conn = null;
Statement stmt = null;
Class.forName("com.mysql.jdbc.Driver");
String dburl = "jdbc:mysql://localhost:3306/hp";
String dbuser = "root";
String dbpassword = "19920102";
conn = DriverManager.getConnection(dburl, dbuser, dbpassword);
java.sql.Statement st = conn.createStatement();
String sql = "select* from xsgli";
ResultSet rs = st.executeQuery(sql);
Vector columnVector = getColumnVector();
Vector dataVector = getDataVector();
final JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane, BorderLayout.CENTER);
table = new JTable(dataVector, columnVector);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
scrollPane.setViewportView(table);
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Stmessege1 extends JFrame {
private static final long serialVersionUID = 1L;
private JTable table;// 声明表格
void Stmessege11() {
setSize(460, 320);
setVisible(true);
}
public Vector getDataVector() throws SQLException, ClassNotFoundException {
Connection conn = null;
java.sql.Statement stmt = null;
Class.forName("com.mysql.jdbc.Driver");
String dburl = "jdbc:mysql://localhost:3306/hp";
String dbuser = "root";
String dbpassword = "19920102";
conn = DriverManager.getConnection(dburl, dbuser, dbpassword);
stmt = conn.createStatement();
String sql = "select* from xsgli";
ResultSet rs = stmt.executeQuery(sql);
Vector dataVector = new Vector();
while (rs.next()) {
Vector rowVector = new Vector();
for (int i = 1; i <= 7; i++) {
if (i == 2 || i == 4) {
rowVector.add(String.valueOf(rs.getInt(i)));
} else {
rowVector.add(rs.getString(i));
}
}
dataVector.add(rowVector);
}
return dataVector;
}
public Vector getColumnVector() {
Vector columnVector = new Vector();
columnVector.add("姓名");
columnVector.add("学号");
columnVector.add("性别");
columnVector.add("班级");
columnVector.add("系别");
columnVector.add("成绩");
columnVector.add("专业");
return columnVector;
}
public Stmessege1() throws SQLException, ClassNotFoundException {
super();
setTitle("学生学籍数据显示");
setBounds(100, 100, 382, 223);
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Connection conn = null;
Statement stmt = null;
Class.forName("com.mysql.jdbc.Driver");
String dburl = "jdbc:mysql://localhost:3306/hp";
String dbuser = "root";
String dbpassword = "19920102";
conn = DriverManager.getConnection(dburl, dbuser, dbpassword);
java.sql.Statement st = conn.createStatement();
String sql = "select* from xsgli";
ResultSet rs = st.executeQuery(sql);
Vector columnVector = getColumnVector();
Vector dataVector = getDataVector();
final JScrollPane scrollPane = new JScrollPane();
getContentPane().add(scrollPane, BorderLayout.CENTER);
table = new JTable(dataVector, columnVector);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
scrollPane.setViewportView(table);
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先用jdbc连接数据库,查询得到结果集,然后循环遍历结果集,输出各个字段值。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用jdbc的api或者Hibernate框架~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
JDBC 够你学的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询