liwei2018
发贴: 14
积分: 0
|
于 2005-07-02 14:03
/* * 创建日期 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()); }
} }
|