怎么用java在表单上把数据库的数据显示出来
4个回答
展开全部
1.提取单条记录
//import java.sql.*;
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:%%1";
con=DriverManager.getConnection(url,%%2,%%3);
stmt=conn.createStatement();
stmt.executeUpdate(%%4);
rs=stmt.executeQuery(%%5);
}catch(Exception e){
e.printStackTrace();
}
finally{
try {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
3.显示表格
/*
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import javax.swing.table.*;
*/
String[] colHeads=%%4;
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:%%1";
conn=DriverManager.getConnection(url,%%2,%%3);
stmt=conn.createStatement();
rs=stmt.executeQuery("SELECT count(*) as au_count from "+%%5);
rs.next();
int iCount=rs.getInt("au_count");
Object[][] data=new Object[iCount][];
int i=0;
rs=stmt.executeQuery("SELECT * from "+%%5);
while(rs.next()){
data[i]=new Object[iCount];
data[i][0]=rs.getString("au_fname");
data[i][1]=rs.getString("Phone");
data[i][2]=rs.getString("City");
i++;
}
JTable table=new JTable(data,colHeads);
JScrollPane jsp=new JScrollPane(table);
getContentPane().add(jsp);
}catch(Exception e){
e.printStackTrace();
}
finally{
try {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
6.关闭时关闭连接
//import java.sql.*;
addWindowListener(new WindowAdapter{
public void windowClosing(WindowEvent wevent){
if(stmt!=null){
try {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
7.执行命令
//import java.sql.*;
Connection conn=null;
PreparedStatement pst=null;
try {
conn=DriverManager.getConnection(url);
pst=conn.prepareStatement("Insert Into grade(%%1) Values (?)");
pst.setInt(1,%%2);
//pst.setString(2,%%2);
pst.addBatch();
pst.executeBatch();
} catch (SQLException e){
e.printStackTrace();
}
finally{
try {
if (pst != null)
pst.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//import java.sql.*;
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:%%1";
con=DriverManager.getConnection(url,%%2,%%3);
stmt=conn.createStatement();
stmt.executeUpdate(%%4);
rs=stmt.executeQuery(%%5);
}catch(Exception e){
e.printStackTrace();
}
finally{
try {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
3.显示表格
/*
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import javax.swing.table.*;
*/
String[] colHeads=%%4;
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:%%1";
conn=DriverManager.getConnection(url,%%2,%%3);
stmt=conn.createStatement();
rs=stmt.executeQuery("SELECT count(*) as au_count from "+%%5);
rs.next();
int iCount=rs.getInt("au_count");
Object[][] data=new Object[iCount][];
int i=0;
rs=stmt.executeQuery("SELECT * from "+%%5);
while(rs.next()){
data[i]=new Object[iCount];
data[i][0]=rs.getString("au_fname");
data[i][1]=rs.getString("Phone");
data[i][2]=rs.getString("City");
i++;
}
JTable table=new JTable(data,colHeads);
JScrollPane jsp=new JScrollPane(table);
getContentPane().add(jsp);
}catch(Exception e){
e.printStackTrace();
}
finally{
try {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
6.关闭时关闭连接
//import java.sql.*;
addWindowListener(new WindowAdapter{
public void windowClosing(WindowEvent wevent){
if(stmt!=null){
try {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
7.执行命令
//import java.sql.*;
Connection conn=null;
PreparedStatement pst=null;
try {
conn=DriverManager.getConnection(url);
pst=conn.prepareStatement("Insert Into grade(%%1) Values (?)");
pst.setInt(1,%%2);
//pst.setString(2,%%2);
pst.addBatch();
pst.executeBatch();
} catch (SQLException e){
e.printStackTrace();
}
finally{
try {
if (pst != null)
pst.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
将数据库连接获取数据的方法返回list类型,在前台初始化对象,迭代显示到前台页面就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
后台返回List或者数组 前台迭代
追问
能不能具体点,最好可写出代码为例
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询