Java教程

链接操作数据库

本文主要是介绍链接操作数据库,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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();
}
  

这篇关于链接操作数据库的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!