使用数据库连接在java中交换行和列

时间:2016-01-25 19:12:20

标签: java mysql database-connectivity

我的MySQL表格如下:

UIN  Body_Type   Brand     Model   Variant
1     Sedan      Skoda     Rapid    MPI
2    Hatchback   Ford      Fiesta   TDC
3      SUV       Renault   Duster   RXE

但在java中,我希望它显示为:

Fields         Car1         Car2          Car3
UIN            1            2             3
Body_Type      Sedan        Hatchback     SUV
Brand          Skoda        Ford          Renault
Model          Rapid        Fiesta        Duster
Variant        MPI          TDC           RXE

我试过这段代码:

String a=tcar1.getText();
String b=tcar2.getText();
String c=tcar3.getText();
//to get UIN of cars

javax.swing.table.DefaultTableModel m =(javax.swing.table.DefaultTableModel) tab.getModel();
m.setRowCount(0);
        try{
                Class.forName("java.sql.DriverManager");
                java.sql.Connection l=java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/Cars", "root","root");
                java.sql.Statement s=l.createStatement();
                String update="select * from cars where UIN="+a+" or UIN="+b+" or UIN="+c+";";
                java.sql.ResultSet count=s.executeQuery(update);
                java.sql.ResultSet count1=FlipTableConverters.fromResultSet(count);
                while(count1.next()){
                    String s1 = count.getString(1);
                    String s2 = count.getString(2);
                    String s3 = count.getString(3);
                    String s4 = count.getString(4);
                    m.addRow(new String[]{s1,s2,s3,s4});
            }
          }catch(Exception u){
            javax.swing.JOptionPane.showMessageDialog(this, "Error "+u);
          }

代码在&FlipTableConverters'中显示和出错。 我是学习Java的学生,这段代码是我项目的一部分。请帮忙。

0 个答案:

没有答案