链接操作数据库
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="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url="jdbc:sqlserver://localhost:1433; DatabaseName=student","lys","123456";
//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);
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(pstmt);
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();
}
public static void main(String args[])
{
Showmessage sh=new Showmessage();
sh.showinfo();
}
}