新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
商品最好存储在数据库中, 商业应用哪有存在文件里的, 也就是学校练习使用文件存储。
洪山网站建设公司创新互联,洪山网站设计制作,有大型网站制作公司丰富经验。已为洪山上千余家提供企业网站建设服务。企业网站搭建\外贸营销网站建设要多少钱,请找那个售后服务好的洪山做网站的公司定做!
这是一段增删改查的代码, 将里面的MSC类修改成商品类就行了.(既然学过java, 你应该会修改吧)
数据库使用mysql(小型) 或sql server(大型) 均可:建议使用mysql即可
代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
class MSC
{
public String MscID;
public String MscName;
public String MscCompany;
public float MscLongitude;
public float MscLatitude;
public float MscAltitude;
public MSC(String MscID, String MscName, String MscCompany,
float MscLongitude, float MscLatitude,float MscAltitude){
this.MscID = MscID;
this.MscName = MscName;
this.MscCompany = MscCompany;
this.MscLongitude =MscLongitude;
this.MscLatitude = MscLatitude;
this.MscAltitude = MscAltitude;
}
}
public class sqlserverjdbc {
public Connection getConnection(){
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=gsm"; //连接服务器和数据库sample
String userName = "sa"; //默认用户名
String userPwd = "123"; //密码
Connection dbConn = null;
try {
Class.forName(driverName);
dbConn =DriverManager.getConnection(dbURL, userName, userPwd);
} catch (Exception e) {
e.printStackTrace();
}
return dbConn;
}
public void printUserInfo(){
Connection con = getConnection();
Statement sta = null;
ResultSet rs = null;
System.out.println("打印表格MSC信息");
try {
sta = con.createStatement();
rs = sta.executeQuery("select * from MSC信息");
System.out.println("MscID\tMscName\tMscCompany\tMscLongitude\tMscLatitude\tMscAltitude");
while(rs.next()){
System.out.println(rs.getString("MscID")+"\t"+
rs.getString("MscName")+"\t"+
rs.getString("MscCompany")+"\t"+
rs.getFloat("MscLongitude")+"\t"+
rs.getFloat("MscLatitude")+"\t"+
rs.getFloat("MscAltitude"));
}
con.close();
sta.close();
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("打印完成\n");
}
public void delete(String MscID){
Connection con = getConnection();
String sql = "delete from MSC信息 where MscID = " + MscID;
PreparedStatement pst;
System.out.println("删除表格MSC信息中 ID = "+MscID+"的记录");
try {
pst = con.prepareStatement(sql);
pst.execute();
pst.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("记录删除失败!!!");
}
System.out.println("记录删除成功!!!\n");
}
public void insert(MSC msc){
Connection con = getConnection();
String sql = "insert into MSC信息 values(?,?,?,?,?,?)";
PreparedStatement pst;
System.out.println("插入一条记录");
try {
pst = con.prepareStatement(sql);
pst.setString(1, msc.MscID);
pst.setString(2, msc.MscName);
pst.setString(3, msc.MscCompany);
pst.setFloat(4, msc.MscLongitude);
pst.setFloat(5, msc.MscLatitude);
pst.setFloat(6, msc.MscAltitude);
pst.execute();
pst.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("插入失败!!!");
}
System.out.println("插入成功!!!\n");
}
//更新MscID的MscName
public void updateMscName(String MscID, String MscName){
Connection con = getConnection();
String sql = "update MSC信息 set MscName = ? where MscID = ?";
PreparedStatement pst;
System.out.println("修改一条记录");
try {
pst = con.prepareStatement(sql);
pst.setString(1, MscName);
pst.setString(2, MscID);
pst.execute();
pst.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("修改失败!!!");
}
System.out.println("修改成功!!!\n");
}
public static void main(String args[]){
sqlserverjdbc sql = new sqlserverjdbc();
sql.printUserInfo();
sql.delete("1111");
sql.printUserInfo();
sql.updateMscName("5215", "联想");
sql.printUserInfo();
sql.insert(new MSC("1111", "中兴" ," 中兴", (float)12.2, (float)3.4,(float)45.5));
sql.printUserInfo();
}
}
把这一段放在一个循环里,然后判断是否继续输入的值,如果继续,那就继续下一次循环,如果停止,则跳出循环
计算次数放在小循环里,每输入一次加1,在大循环里定义,每次大循环都会从0开始
while(true){
int count = 0;
int number = rand.nextInt(100);
System.out.print("Your guess?");
int guess = console.nextInt();
do{
if (guess number){
count++;
System.out.println("It's lower.");
System.out.print("Your guess?");
guess = console.nextInt();
i++;
}else if (guess number){
System.out.println("It's higher.");
System.out.print("Your guess?");
guess = console.nextInt();
i++;
}
}while(guess != number);
if(guess == number){
System.out.println("You got it right in " + i + " guesses!");
System.out.println("Do you want to play again?");
String line = console.nextLine();
if (line.startsWith("Y")||line.startsWith("y")) {
guess();
}else if (line.startsWith("N")||line.startsWith("n")) {
break;
}
}
}
importjava.io.IOException;/××只能给当前用户赋予对该文件的权限,调用createNewFile()方法默认的权限是644.×/publicclassFilePermission{publicstaticvoidmain(String[]args){try{Filefile=newFile("/home/test3.txt");if(
interface Poker{
int ACE = 1;
int JACK = 11;
int QUEEN = 12;
int KING = 13;
int CLUBS = 1; //草花最小
int DIAMONDS = 2; //方块
int HEARTS = 3; //红桃
int SPADES = 4; //黑桃最大
public int rank();
public String toString();
}
class Card implements Poker{
private int value; //value表示牌值:1--13
private int suit; //suit表示花色:1--4
public Card(int v,int s) //构造方法:设置牌值和花色
{ value = v; suit = s; }
public int getValue() //返回牌值
{ return value; }
public int getSuit() //返回花色
{ return suit; }
public int rank() //返回牌的排序
{ return 4*(value-1) +suit; }
public String toString() //将牌值和花色转换成字符串
{ String v;
String s;
if (value == ACE) v="A";
else if (value == JACK) v= "J";
else if (value == QUEEN) v="Q";
else if (value == KING) v="K";
else v=String.valueOf(value);
if (suit == CLUBS) s = "草花";
else if (suit == DIAMONDS) s = "方块";
else if (suit == HEARTS) s = "红桃";
else s = "黑桃";
return s + " "+v;
}
}
class CardDeck {
private Card deck []; //deck[]存放一付牌
private int cards; //cards表示还剩几张牌
public CardDeck() //构造方法:创建52张牌,填上牌值和花色,洗牌
{ deck = new Card[52]; //创建52张牌
fill(); //填上牌值和花色
shuffle(); //洗牌
}
public void fill() //填上牌值和花色
{ int i; int j;
for (i =1; i = 13; i++)
for (j = 1; j = 4; j++)
deck[4*(i-1) + j - 1]= new Card(i,j);
// deck[0]=草花A,deck[1]=方块A,deck[2]=红桃A,deck[3]=黑桃A,deck[4]=草花2...
cards = 52; //开始有52张牌
}
public void shuffle() //洗牌
{
int next;
for (next = 0 ; next cards-1; next++) {
int r=(int) (Math.random()*52);
//随机生成0--51的一个数
Card temp = deck [next]; //两张牌交换位置
deck[next] = deck[r];
deck[r] = temp;
}
}
public Card draw() //发一张牌
{
if (cards == 0) return null;
cards--;
return deck[cards];
}
}
class CardDeck2 extends CardDeck{
public CardDeck2(){
super();
}
public void shuffle(){
for(int i = 0; i 10; i ++){
super.shuffle();
}
}
public Card[] draw2(){
Card[] card = new Card[2];
for(int i = 0; i 2; i ++){
card[i] = super.draw();
}
return card;
}
public boolean compare(Card[] yours, Card[] mine){
int valuecountyou = 0;
int valuecountmine = 0;
int colorcountyou = 0;
int colorcountmine = 0;
for(int i = 0; i yours.length; i ++){
valuecountyou += yours[i].getValue();
valuecountmine += mine[i].getValue();
colorcountyou += yours[i].getSuit();
colorcountmine += mine[i].getSuit();
}
if(valuecountyou valuecountmine){
return true;
}else if(valuecountyou valuecountmine){
return false;
}else{
if(colorcountyou colorcountmine){
return true;
}else
return false;
}
}
}
public class Test2 {
public static void main(String args[]){
CardDeck2 d = new CardDeck2();
int i;
int wins = 0; //wins表示你赢的次数
int rounds = 10; //rounds表示玩牌次数
for (i =1; i=rounds; i++){
Card[] yours = d.draw2();
System.out.print("您的牌是: " + yours[0]+" ," + yours[1] + " ,");
Card[] mine = d.draw2();
System.out.print("我的牌是:" + mine[0]+" 。 " + mine[1]+" 。 ");
if (d.compare(yours, mine)){
System.out.println("恭喜,您赢了!");
wins++;
}else{
System.out.println("抱歉,我赢了。");
}
}
System.out.println("您总共赢了:"+ wins+" 次");
}
}