您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 其它行业文档 > Java数据库操作大全文库
Java数据库操作大全文库.txt和英俊的男人握握手,和深刻的男人谈谈心,和成功的男人多交流,和普通的男人过日子。1.提取单条记录/*importjava.sql.*;privateConnectionconn=null;privateStatementstmt=null;privateResultSetrs=null;*/try{Class.forName(sun.jdbc.odbc.JdbcOdbcDriver);Stringurl=jdbc:odbc:%%1;conn=DriverManager.getConnection(url,%%2,%%3);stmt=conn.createStatement();rs=stmt.executeQuery(%%4);rs.next();int%%5=rs.getInt(%%6);//String%%5=rs.getString(%%6);}catch(Exceptione){e.printStackTrace();}finally{try{if(rs!=null)rs.close();if(stmt!=null)stmt.close();if(conn!=null)conn.close();}catch(SQLExceptione){e.printStackTrace();}}2.单值比较/*importjava.sql.*;privateConnectionconn=null;privateStatementstmt=null;privateResultSetrs=null;*/try{Class.forName(sun.jdbc.odbc.JdbcOdbcDriver);Stringurl=jdbc:odbc:%%1;conn=DriverManager.getConnection(url,%%2,%%3);stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);rs=stmt.executeQuery(%%2);rs.next();int%%6=rs.getInt(%%7);if(%%6==%%4){%%5}}catch(Exceptione){e.printStackTrace();}finally{try{if(rs!=null)rs.close();if(stmt!=null)stmt.close();if(conn!=null)conn.close();}catch(SQLExceptione){e.printStackTrace();}}3.显示表格/*importjava.awt.*;importjavax.swing.*;importjava.sql.*;importjavax.swing.table.*;privateConnectionconn=null;privateStatementstmt=null;privateResultSetrs=null;*/String[]colHeads=newString[]{au_fname,Phone,City};try{Class.forName(sun.jdbc.odbc.JdbcOdbcDriver);Stringurl=jdbc:odbc:%%1;conn=DriverManager.getConnection(url,%%2,%%3);stmt=conn.createStatement();rs=stmt.executeQuery(SelectCount(*)Asau_countFrom%%5);rs.next();intiCount=rs.getInt(au_count);Object[][]data=newObject[iCount][];inti=0;rs=stmt.executeQuery(Select*From%%5);while(rs.next()){data[i]=newObject[iCount];data[i][0]=rs.getString(au_fname);data[i][1]=rs.getString(Phone);data[i][2]=rs.getString(City);i++;}JTabletable=newJTable(data,colHeads);JScrollPanejsp=newJScrollPane(table);this.add(jsp);}catch(Exceptione){e.printStackTrace();}finally{try{if(rs!=null)rs.close();if(stmt!=null)stmt.close();if(conn!=null)conn.close();}catch(SQLExceptione){e.printStackTrace();}}4.操作表格/*importjava.awt.*;importjavax.swing.*;importjava.sql.*;importjavax.swing.table.*;privateConnectionconn=null;privateStatementstmt=null;privateResultSetrs=null;*/JTabletable;Object[][]data;JButtonjb;JScrollPanejsp;JComboBoxjcb;JCheckBoxjchb;jcb=newJComboBox();jchb=newJCheckBox();jcb.addItem(男);jcb.addItem(女);String[]colHeads={id,性别,是否};try{Class.forName(sun.jdbc.odbc.JdbcOdbcDriver);Stringurl=jdbc:odbc:%%1;conn=DriverManager.getConnection(url);stmt=conn.createStatement();rs=stmt.executeQuery(SelectCount(id)AscountFrom%%2);rs.next();intiCount=rs.getInt(count);data=newObject[iCount][];inti=0;rs=stmt.executeQuery(Select*From%%2);while(rs.next()){data[i]=newObject[iCount];data[i][0]=rs.getString(id);data[i][1]=rs.getString(gender);data[i][2]=rs.getString(isMember);i++;}table=newJTable();DefaultTableModeldt=newDefaultTableModel(data,colHeads);//table.setModel(dt);jsp=newJScrollPane(table);this.add(jsp,BorderLayout.CENTER);TableColumntc=table.getColumn(性别);TableColumntc2=table.getColumn(是否);tc.setCellEditor(newDefaultCellEditor(jcb));tc2.setCellEditor(newDefaultCellEditor(jchb));}catch(Exceptione){e.printStackTrace();}finally{try{if(rs!=null)rs.close();if(stmt!=null)stmt.close();if(conn!=null)conn.close();}catch(SQLExceptione){e.printStackTrace();}}JPaneljp2=newJPanel();this.add(jp2,BorderLayout.NORTH);JPaneljp=newJPanel(newFlowLayout());jp2.add(jp);jp.add(newJTextField(10));5.数值范围查询/*importjava.sql.*;importjavax.swing.*;importjava.awt.*;importjavax.swing.table.*;privateConnectionconn=null;privatePreparedStatementpst=null;privateResultSetrs=null;*/String[]colHeads=newString[]{id,name,price};JScrollPanejsp;JTabletable;Object[][]data;intmin=Integer.parseInt(jcb1.getSelectedItem().toString());intmax=Integer.parseInt(jcb2.getSelectedItem().toString());Stringsql=SelectCount(*)Aspro_countFrom%%2Where%%3Between?And?;try{conn=DriverManager.getConnection(url);Stringurl=jdbc:odbc:%%1;pst=conn.prepareStatement(sql);pst.setInt(1,min);pst.setInt(2,max);rs=pst.executeQuery();rs.next();intiCount=rs.getInt(pro_count);Object[][]data=newObject[iCount][];pst=conn.prepareStatment(Select*From%%2Where%%3Between?And?);inti=0;while(rs.next()){data[i]=newObject[3];data[i][0]=rs.getString(id);data[i][1]=rs.getString(name);data[i][2]=rs.getString(price);i++;}table=newJTable();DefaultTableModeldt=newDefaultTableModel(data,colHeads);table.setModel(dt);jsp=newJScrollPane(table);this.add(jsp,BorderLayout.CENTER);}catch(Exceptione1){e1.printStackTrace();}finally{try{if(rs!=null)rs.close();if(pst!=null)pst.close();if(conn!=null)conn.close();}catch(SQLExceptione){e.printStackTrace();}}6.关闭时关闭连接//importjava.sql.*;addWindowListener(newWindowAdapter{publicvoidwindowClosing(WindowEventwevent){if(stmt!=null){try{if(rs!=null)rs.close();if(stmt!=null)stmt.close();if(conn!=null)conn.close();}catch(SQLExceptione){e.printStackTrace();}}}}7.执行命令/*importjava.sql.*;privateConnectionconn=null;privatePreparedStatementpst=null;*/tr
本文标题:Java数据库操作大全文库
链接地址:https://www.777doc.com/doc-5453913 .html