java连接数据库,Jtabel显示方法
2014-06-16 16:40
381 查看
<span style="font-family:KaiTi_GB2312;font-size:18px;"> </span>
<span style="font-family:KaiTi_GB2312;font-size:18px;"> </span>
<span style="font-family:KaiTi_GB2312;font-size:18px;">package BookManagerSoft;</pan>
<span style="font-family:KaiTi_GB2312;font-size:18px;"> //要导入该包,里面包含了java程序与数据库连接的接口 import java.sql.*; importjava.util.Vector; importjavax.swing.JTable; importjavax.swing.table.TableModel; class JDBCLinker { // TODO Auto-generated methodstub public String strCon ="jdbc:odbc:mydata";//数据库的连接方式mydata是要连接的数据库,连接后程序可以访问该数据库 public String strUser = "sa"; //程序的登录方式,数据库的身份 public String strPwd= "123456"; //登录上述身份的密码 public Connection con=null; //数据库连接累的一个实例 public Statement sta = null; //Statement 的实例 public ResultSet rs=null; //里面存放了,执行查询语句的结果 public JDBCLinker(Stringconn,String user,String passwd){//构造方法 strCon=conn; strUser=user; strPwd=passwd; } public JDBCLinker(){//无参构造(可不写某人以父类型是存在) } public voidlinkStructerQueryLanguage(){//重要.连接数据库里面会有很多方法会抛出SQLException,可以处理,也可以继续向上抛出,步骤式的代码很重要. try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//加载驱动器.注意在项目下面应该添加数据库驱动连接.jar //我加载的是mysql-connector-java-5.1.22-bin.jar,这是针对项目的 } catch (ClassNotFoundExceptione) { System.out.println("创建数据库失败"); // TODO Auto-generated catchblock e.printStackTrace(); } //获得连接 try{ con =DriverManager.getConnection(strCon,strUser,strPwd);//创建连接 }catch(SQLExceptione){ System.out.println("连接数据库异常"); } System.out.println("连接成功"); try { sta = con.createStatement(); //Statement,由已经连接上的Connection对象创建 } catch (SQLException e){ // TODO Auto-generated catchblock e.printStackTrace(); } } //command 写SQL语句 public String exeUpdate (Stringcommand){ try{ sta.executeUpdate(command); return "prefect"; }catch(Exceptione){ e.printStackTrace(); returne.getStackTrace().toString(); } } //command 写SQL语句,返回值为ResultSet对象,在程序中对其分析 public ResultSetexeQuery(String command){ try { returnrs=sta.executeQuery(command); } catch (SQLException e){ // TODO Auto-generated catchblock e.printStackTrace(); return null; } } //关闭数据库对象,关闭连接对象 public voidcloseLink(){ try{ sta.close(); con.close(); }catch(Exceptione){ e.printStackTrace(); } } //此处主要是对ResultSet对象的方式 public voidprintResult(){ int i; try { String str=null; while(rs.next()){//如果rs可以存在时,才可以正常执行next();,否则抛出异常,说明没有下一行 for(i=1;true;i++){ try{ //没有rs.getString(i);,是抛出异常,此时跳出循环进行下次的读取 str=rs.getString(i); System.out.print(str+" "); }catch(Exceptione){ break;//根据异常等价条件进行退出 } } System.out.println(""); } } catch (SQLException e){ // TODO Auto-generated catchblock e.printStackTrace(); System.out.println("没有什么结果!"); } } //将rs数据结果,存放到JTable并返回该对象 public JTablecreateTable(){ int i; int j = 1; Vector headername=new Vector(); Vector> tablemode =newVector(); headername.add("1234"); headername.add("afjkd"); try { String str=null; while(rs.next()){ tablemode.add(newVector()); for(i=1;true;i++){ try{ str=rs.getString(i); tablemode.elementAt(j).add(str); }catch(Exceptione){ break;//根据异常等价条件进行退出 } } j++; } return newJTable(tablemode,headername); } catch (SQLException e){ // TODO Auto-generated catchblock //e.printStackTrace(); System.out.println("没有什么结果!"); return null; } } //将rs数据结果存放到table.getModel();之中 public voidresetTableModel(JTable table){ int i=1; int j = 1; TableModeltm=table.getModel(); try { String str=null; while(rs.next()){ for(i=1;true;i++){ try{ str=rs.getString(i); tm.setValueAt(str,j-1,i-1); }catch(Exceptione){ break;//根据异常等价条件进行退出 } } j++; } for(;j<=table.getRowCount();j++) for(i=0;i tm.setValueAt("",j-1,i); } catch (SQLException e){ // TODO Auto-generated catchblock //e.printStackTrace(); System.out.println("没有什么结果!"); } } }</span>
相关文章推荐
- JAVA与数据库连接方法(一)
- JAVA与数据库连接方法(二)
- 使用Java程序连接各种数据库的方法介绍
- JAVA与数据库连接方法(三)
- (强烈推荐)java连接oracle数据库的各种方法及java在数据库中的含义
- JAVA连接数据库方法
- Java连接各种数据库方法
- JAVA与数据库连接方法(一)
- java连接各种数据库方法
- 使用Java程序连接各种数据库的方法
- java连接oracle数据库的各种方法及java在数据库中的含义
- java连接数据库的方法
- java连接oracle数据库的各种方法及java在数据库中的含义
- java连接oracle数据库的各种方法及java在数据库中的含义(转载'SunnyXu ')
- (强烈推荐)java连接oracle数据库的各种方法及java在数据库中的含义(SunnyXu原创)
- Java做信息管理系统的数据库连接方法
- 使用Java程序连接各种数据库的方法介绍
- JAVA与数据库连接方法(二)[转]
- JAVA与数据库连接方法(三) [转]
- java 中如何在JTABLE中显示连接数据库表中的数据