日期:2014-05-18  浏览次数:20514 次

如何将连接mysql的java程序改成连接sql severe 2005连接的呀
我的程序是用mysql
可是老师要求用sql severe
求各位好心的大神帮帮小妹呀
拜托了~~

package edu.ncsu.cs326.coffeemaker.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import edu.ncsu.csc326.coffeemaker.Recipe;
import edu.ncsu.csc326.coffeemaker.exceptions.RecipeException;
//添加一个菜单
public class RecipeBookDB {

public static boolean addRecipe(Recipe r) {
DBConnection db = new DBConnection();
Connection conn = null;
PreparedStatement stmt = null;
boolean recipeAdded = false;
try {
conn = db.getConnection();
stmt = conn.prepareStatement("INSERT INTO recipe_book VALUES(?,?,?,?,?,?)");
stmt.setString(1, r.getName());
stmt.setInt(2, r.getPrice());
stmt.setInt(3, r.getAmtCoffee());
stmt.setInt(4, r.getAmtMilk());
stmt.setInt(5, r.getAmtSugar());
stmt.setInt(6, r.getAmtChocolate());
int updated = stmt.executeUpdate();
if (updated == 1) {
recipeAdded = true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnection.closeConnection(conn, stmt);
}
return recipeAdded;
}
//删除菜单
public static String deleteRecipe(String name) {
DBConnection db = new DBConnection();
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = db.getConnection();
stmt = conn.prepareStatement("DELETE FROM recipe_book WHERE name=?");
stmt.setString(1, name);
int result = stmt.executeUpdate(); 
if (result == 0) { //Nothing was deleted
name = null;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnection.closeConnection(conn, stmt);
}
return name;
}

public static String editRecipe(String name, Recipe r) {
DBConnection db = new DBConnection();
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = db.getConnection();
stmt = conn.prepareStatement("UPDATE recipe_book SET price=?, amtCoffee=?, amtMilk=?, amtSugar=?, amtChocolate=? WHERE name=?");
stmt.setInt(1, r.getPrice());
stmt.setInt(2, r.getAmtCoffee());
stmt.setInt(3, r.getAmtMilk());
stmt.setInt(4, r.getAmtSugar());
stmt.setInt(5, r.getAmtChocolate());
stmt.setString(6, name);
int result = stmt.executeUpdate();
if (result == 0) { //nothing was updated
name = null;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnection.closeConnection(conn, stmt);
}
return name;
}

public static Recipe getRecipe(String name) throws RecipeException {
DBConnection db = new DBConnection();
Connection conn = null;
PreparedStatement stmt = null;
Recipe r = null;
try {
conn = db.getConnection();
stmt = conn.prepareStatement("SELECT * FROM recipe_book WHERE name=?");
stmt.setString(1, name);
ResultSet rs = stmt.executeQuery();
//Only one result b/c name is primary key
if (rs.next()) {
r = new Recipe();
r.setName(name);
r.setPrice(rs.getString("price"));
r.setAmtCoffee(rs.getString("amtCoffee"));
r.setAmtMilk(rs.getString("amtMilk"));
r.setAmtSugar(rs.getString("amtSugar"));
r.setAmtChocolate(rs.getString("amtChocolate"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnection.closeConnection(conn, stmt);
}
return r;
}
<