java连接数据库,Jtabel显示方法

来源:互联网 发布:yum install gcc 编辑:程序博客网 时间:2024/06/10 12:45
<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 methodstubpublic 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 catchblocke.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 catchblocke.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 catchblocke.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 catchblocke.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;itm.setValueAt("",j-1,i);} catch (SQLException e){// TODO Auto-generated catchblock//e.printStackTrace();System.out.println("没有什么结果!");}}}</span>


0 0
原创粉丝点击