当前位置: 代码迷 >> Java相关 >> java访问sql数据库问题。。
  详细解决方案

java访问sql数据库问题。。

热度:422   发布时间:2010-05-17 14:06:40.0
java访问sql数据库问题。。
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class DBApplication extends Frame implements ActionListener
{
       Connection con;
       Statement stmt;
       ResultSet rs;
       Button Query,Update,Delete,increase,show,exit;
       TextField T1,T2;
       Label L1,L2;
       String bookname,booknum;
       String str1,str2;
       Panel p1,p2;

      
    public DBApplication() {
        super("简单数据库应用程序");
        Query=new Button("查询");
        Update=new Button("更新");
        Delete=new Button("删除");
        increase=new Button("添加");
        show=new Button("显示");
        exit=new Button("退出");
        T1=new  TextField();      
        T2=new  TextField();
        T1.setBackground(Color.GREEN);
        T2.setBackground(Color.GREEN);
        L1=new Label("图书号");
        L2=new Label("书名");
        p1=new Panel();
        p2=new Panel();
        p1.setLayout(new GridLayout(2,2,40,10));
        p2.setLayout(new GridLayout(2,3,40,10));
        this.setLayout(new GridLayout(2,1));
        p1.add(L1);
        p1.add(T1);
        p1.add(L2);
        p1.add(T2);
        p2.add(show);
        p2.add(Query);
        p2.add(Update);
        p2.add(Delete);
        p2.add(increase);
        p2.add(exit);
        this.add(p1);
        this.add(p2);
        Query.addActionListener(this);
        Update.addActionListener(this);
        increase.addActionListener(this);
        Delete.addActionListener(this);
        show.addActionListener(this);
        exit.addActionListener(this);
        pack();
        this.setBackground(Color.BLUE);
        this.setVisible(true);
        try
        {
          Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
          con=DriverManager.getConnection("jdbc:sqlserver://lyb-PC:1433;DatabaseName=LIB_M","","");
          stmt=con.createStatement();
        }
        catch(Exception e){}
    }
   
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==show)
{
   try
   {
   rs=stmt.executeQuery("select 图书号,书名  from bookdata");
   while(rs.next())
   {
   bookname=rs.getString("书名");
   booknum =rs.getString("图书号");      
   T1.setText(booknum);
   T2.setText(bookname);
   }
     System.out.println("已访问完");
     rs.close();     
   }
   catch(Exception e1){}
}
if(e.getSource()==Query)
{
try{
    int flag;
    rs=stmt.executeQuery("select 图书号,书名 from bookdata");
    while(rs.next())
    {
        bookname=rs.getString("书名");
        booknum =rs.getString("图书号");      
        T1.setText(booknum);
        T2.setText(bookname);      
    }
}
  catch(Exception e2){}
}
if(e.getSource()==Update)
{
try{
  str1=T1.getText().trim();
  str2=T2.getText().trim();
  String strupd="update bookdata set 图书号='"+str1+" 'where 书名='"+str2+"'";
  stmt.executeUpdate(strupd);        
}
catch(Exception e3){}
}
if(e.getSource()==increase)
{
  try{
  str1=T1.getText();
  str2=T2.getText();
  String strinc="insert into bookdata(图书号,书名) values(str1,str2)";
  stmt.executeUpdate(strinc);
  }
  catch(Exception e4){}
}
if(e.getSource()==Delete)
{
try
{
str1=T1.getText();
String strdel="delete * from bookdata where 图书号=str1";
stmt.executeUpdate(strdel);
}
catch(Exception e5){}
}
if(e.getSource()==exit)
{
     try
     {
     rs.close();
     stmt.close();
     con.close();
     }
     catch(Exception e6){}
     System.exit(0);
}
}
public static void main(String[] args)
{
  new DBApplication();
}
}
搜索更多相关的解决方案: java  数据库  访问  sql  

----------------解决方案--------------------------------------------------------
  相关解决方案