⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 updatexmlbean.java

📁 java数据库编程源码
💻 JAVA
字号:
package JavaDatabaseBible.ch15;

import java.io.*;
import java.sql.*;
import javax.sql.*;

public class UpdateXMLBean{
  protected static String dbUserName = "sa";
  protected static String dbPassword = "dba";
  protected String xmlHeader = "<?xml version=\"1.0\"?>";
  
  protected int id;
  protected String year;
  protected String price;
  protected String make;
  protected String model;
  protected String color;
  protected String engine;
  protected String transmission;
  protected String body;
  protected String zip;
	
  protected Connection con;
  protected Statement stmt;
  protected ResultSet rs;
  protected ResultSetMetaData md;
  
  public UpdateXMLBean(){
    try{
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    }catch(Exception e){
      e.printStackTrace();
    }
  }
  public void setId(int id){
    this.id=id;
  }
  public int getId(){
    return id;
  }
  public void setYear(String year){
    this.year=year;
  }
  public void setPrice(String price){
    this.price=price;
  }
  public void setColor(String color){
    this.color=color;
  }
  public void setMake(String make){
    this.make=make;
  }
  public void setModel(String model){
    this.model=model;
  }
  public void setEngine(String engine){
    this.engine=engine;
  }
  public void setTransmission(String transmission){
    this.transmission=transmission;
  }
  public void setBody(String body){
    this.body=body;
  }
  public void setZip(String zip){
    this.zip=zip;
  }
  public String getXmlString(){
    String xml = new String(getVehicleData());
    return xml.trim();
  }
  public String updateVehicleData(){
    String status = "Update successful";
    try {
      if(rs.getConcurrency()==ResultSet.CONCUR_UPDATABLE){
        //System.out.println("UPDATABLE");
        //rs.updateInt("id", id);
        rs.updateString("year", year);
        //rs.updateInt("price", price);
        rs.updateString("color", color);
        rs.updateString("make", make);
        rs.updateString("model", model);
        //rs.updateString("body", body);
        //rs.updateString("engine", engine);
        //rs.updateString("transmission", transmission);
        //rs.updateString("zip", zip);
        rs.updateRow();
      }
      else{
        System.out.println("READ_ONLY"); 
        status = "Update failed";
      }
    }catch(Exception e){
      e.printStackTrace();
    }
    return status;
  }
  public byte[] getVehicleData(){
    String rootTag = "VehicleData";
    String SQLQuery = "SELECT color,make,model,body,year FROM Vehicles WHERE id = "+id+";";
                      
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {
      con = DriverManager.getConnection("jdbc:odbc:vehicles");
      stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
				                                   ResultSet.CONCUR_UPDATABLE);
      rs = stmt.executeQuery(SQLQuery);
      md = rs.getMetaData();
      if(rs.getConcurrency()==ResultSet.CONCUR_UPDATABLE){
        System.out.println("UPDATABLE");
      }else{
        System.out.println("READ_ONLY"); 
      }
      os.write(xmlHeader.getBytes());
      os.write(("<"+rootTag+">").getBytes());
      
      int val = 0;
      String xml = "";
      String label = "";
      String value = "";
      int columns = md.getColumnCount();
      rs.next();
      for(int i=1;i<=columns;i++){
        if(md.getColumnType(i)==Types.VARCHAR){
          label = md.getColumnLabel(i);
          value = rs.getString(i);
          xml="<"+label+">"+value+"</"+label+">";
          os.write(xml.getBytes());
        }else if(md.getColumnType(i)==Types.INTEGER){
          label = md.getColumnLabel(i);
          val = rs.getInt(i);
          xml="<"+label+">"+val+"</"+label+">";
          os.write(xml.getBytes());
        }
      }
      os.write(("</"+rootTag+">").getBytes());

    }catch(Exception e){
      e.printStackTrace();
    }
    return os.toByteArray();
  }
  public static void main(String[] args){
    UpdateXMLBean bean = new UpdateXMLBean();
    bean.setId(1000);
    System.out.println(new String(bean.getVehicleData()));
    bean.setId(1000);
    bean.setYear("1998");
    bean.setPrice("4,600");
    bean.setColor("Green");
    bean.setMake ("Honda");
    bean.setModel("Civic");
    bean.setBody ("Coupe");
    bean.setZip  ("21144");
    bean.setEngine ("6 Cylinder");
    bean.setTransmission("5 Speed");
    System.out.println(bean.updateVehicleData());
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -