Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Database/JDBC/SQL/JDO/Hibernate » MySQL  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 本机器修改查询数据库的结果,做与数据库的同步时候出错!
liwei2018





发贴: 14
积分: 0
于 2005-07-02 14:03 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
/*
* 创建日期 2005-7-2
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package com.laixi.mysql;

import java.sql.BatchUpdateException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
* @author laixi
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class MySqlTest {
  
  public static void main(String agrs[])throws SQLException{
    
    ResultSet rs = null;
    
    PreparedStatement ps = null;

    String url = "jdbc:mysql://localhost:3306/test";
    
    Connection con;
    
    Statement stmt;
    try {

      Class.forName("com.mysql.jdbc.Driver");

    } catch(java.lang.ClassNotFoundException e) {
      System.err.print("ClassNotFoundException: ");
      System.err.println(e.getMessage());
    }
    
    try {

      con = DriverManager.getConnection(url, "root", "");
      
      con.setAutoCommit(false);

      stmt = con.createStatement();
      
      rs = stmt.executeQuery("SELECT * FROM COFFEES");
      
      while (rs.next()) {
        String name = rs.getString("COF_NAME");
        int id = rs.getInt("SUP_ID");
        float price = rs.getFloat("PRICE");
        int sales = rs.getInt("SALES");
        int total = rs.getInt("TOTAL");
        //System.out.print(name + " " + id + " " + price);
        //System.out.println(" " + sales + " " + total);
      }
      
      rs.updateObject("SUP_ID", "name");
      
      rs.updateRow();

  } catch(BatchUpdateException b) {
    System.err.println("-----BatchUpdateException-----");
    System.err.println("SQLState: " + b.getSQLState());
    System.err.println("Message: " + b.getMessage());
    System.err.println("Vendor: " + b.getErrorCode());
    System.err.print("Update counts: ");
    int [] updateCounts = b.getUpdateCounts();
    for (int i = 0; i < updateCounts.length; i++) {
      System.err.print(updateCounts[i] + " ");
    }
    System.err.println("");

  } catch(SQLException ex) {
    System.err.println("-----SQLException-----");
    System.err.println("SQLState: " + ex.getSQLState());
    System.err.println("Message: " + ex.getMessage());
    System.err.println("Vendor: " + ex.getErrorCode());
  }

  }
}




话题树型展开
人气 标题 作者 字数 发贴时间
7205 本机器修改查询数据库的结果,做与数据库的同步时候出错! liwei2018 2250 2005-07-02 14:03
5875 Re:本机器修改查询数据库的结果,做与数据库的同步时候出错! liwei2018 367 2005-07-02 14:04
5781 Re:本机器修改查询数据库的结果,做与数据库的同步时候出错! ljy0000 86 2005-07-02 14:40
5826 Re:本机器修改查询数据库的结果,做与数据库的同步时候出错! liwei2018 137 2005-07-02 22:38
6224 Re:本机器修改查询数据库的结果,做与数据库的同步时候出错! liwei2018 5 2005-08-01 18:24

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923