java取出数据库中的表,并以表格的形式显示????????
以下是我的代码,请各位帮我看看哈O(∩_∩)O~我现在可以取出数据,也可以显示数据但是如果我表中有空值就会报空指针错误【没有就正常】packagebeans;import...
以下是我的代码,请各位帮我看看哈O(∩_∩)O~
我现在可以取出数据,也可以显示数据
但是 如果我表中有空值 就会报空指针错误【没有就正常】
package beans;
import javax.swing.*;
import java.awt.*;
import java.awt.List;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;
public class showmessage{
String className="oracle.jdbc.driver.OracleDriver";
String url="jdbc:oracle:thin:@localhost:1521:orcl";
String username="supermarket";
String userpass="abc";
Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
public String ss="";
public String[][] showmessage(String tablename){
String[][] playerInfo1=null;
int x=0;
int k=0;
try{
Class.forName(className);
con=DriverManager.getConnection(url,username,userpass);
pstmt=con.prepareStatement("select * from Provide");
rs=pstmt.executeQuery();
k=rs.getMetaData().getColumnCount();
while(rs.next()){
x++;
for(int i=0;i<k;i++){
System.out.println(i);
}
}
String[][] playerInfo=new String[x][k];
int y=-1;
rs=pstmt.executeQuery();
while(rs.next()){
y++;
System.out.println("1111111111111");
for(int i=0;i<k;i++){
ss=rs.getString(i+1).toString();
if(ss==null){
ss="wxl";
}
playerInfo[y][i]=ss.toString();
}
}
playerInfo1=playerInfo;
}catch(Exception e){
System.out.println(e.toString());
}finally{
try{rs.close();}catch(Exception ee){};
try{pstmt.close();}catch(Exception ee){};
try{con.close();}catch(Exception ee){};
}
for(int i=0;i<x;i++){
for(int j=0;j<k;j++){
System.out.println(playerInfo1[i][j].toString());
}
}
return playerInfo1;
}
public void showinfo(String[][] xx){
JFrame f=new JFrame();
String[ ] Names={"姓名","语文","数学","总分"};
JTable table=new JTable(xx,Names);
table.setPreferredScrollableViewportSize(new Dimension(550,30));
JScrollPane scrollPane=new JScrollPane(table);
f.getContentPane().add(scrollPane,BorderLayout.CENTER);
f.setTitle("SimpleTable");
f.pack();
f.show();
}
} 展开
我现在可以取出数据,也可以显示数据
但是 如果我表中有空值 就会报空指针错误【没有就正常】
package beans;
import javax.swing.*;
import java.awt.*;
import java.awt.List;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;
public class showmessage{
String className="oracle.jdbc.driver.OracleDriver";
String url="jdbc:oracle:thin:@localhost:1521:orcl";
String username="supermarket";
String userpass="abc";
Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
public String ss="";
public String[][] showmessage(String tablename){
String[][] playerInfo1=null;
int x=0;
int k=0;
try{
Class.forName(className);
con=DriverManager.getConnection(url,username,userpass);
pstmt=con.prepareStatement("select * from Provide");
rs=pstmt.executeQuery();
k=rs.getMetaData().getColumnCount();
while(rs.next()){
x++;
for(int i=0;i<k;i++){
System.out.println(i);
}
}
String[][] playerInfo=new String[x][k];
int y=-1;
rs=pstmt.executeQuery();
while(rs.next()){
y++;
System.out.println("1111111111111");
for(int i=0;i<k;i++){
ss=rs.getString(i+1).toString();
if(ss==null){
ss="wxl";
}
playerInfo[y][i]=ss.toString();
}
}
playerInfo1=playerInfo;
}catch(Exception e){
System.out.println(e.toString());
}finally{
try{rs.close();}catch(Exception ee){};
try{pstmt.close();}catch(Exception ee){};
try{con.close();}catch(Exception ee){};
}
for(int i=0;i<x;i++){
for(int j=0;j<k;j++){
System.out.println(playerInfo1[i][j].toString());
}
}
return playerInfo1;
}
public void showinfo(String[][] xx){
JFrame f=new JFrame();
String[ ] Names={"姓名","语文","数学","总分"};
JTable table=new JTable(xx,Names);
table.setPreferredScrollableViewportSize(new Dimension(550,30));
JScrollPane scrollPane=new JScrollPane(table);
f.getContentPane().add(scrollPane,BorderLayout.CENTER);
f.setTitle("SimpleTable");
f.pack();
f.show();
}
} 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询