JDBC 练习

import java.sql.*;
import java.util.ArrayList;


import javax.sql.*;


public class DBTest {


Connection connection;
Statement statement;
ResultSet resaultset;


public DBTest() {
super();
connection = null;
statement = null;
resaultset = null;
}


public DBTest(String url, String username, String password) {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("驱动加载成功!");
connection = DriverManager.getConnection(url, username, password);
System.out.println("数据库连接成功!");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


public void DBClose() {
if (connection != null)
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (statement != null)
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (resaultset != null) {
try {
resaultset.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


public ArrayList<Hotel> getMessage(String sql) {


ArrayList<Hotel> arraylist = new ArrayList<Hotel>();
try {
statement = connection.createStatement();
resaultset = statement.executeQuery(sql);
while (resaultset.next()) {
String name = resaultset.getString(1);
String address = resaultset.getString(2);
String telephone = resaultset.getString(3);
String roomtype = resaultset.getString(4);
float roomprice = resaultset.getFloat(5);
Hotel hotel = new Hotel(name, address, telephone, roomtype,
roomprice);
arraylist.add(hotel);
}
resaultset.close();
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return arraylist;
}


public int updateMessage(String sql) {
int num;
try {
statement = connection.createStatement();
num = statement.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
return num;
}


public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/test";
String name = "root";
String password = "123456";
DBTest db = new DBTest(url, name, password);

Hotel h[] = new Hotel[3];
for(int i = 0 ; i<h.length ; i++) {
h[i] = new Hotel("name"+i*10,"address"+i*10,"telephone"+i*10,"roomtype"+i*10,i*10);
String sql_insert = "insert into hotel (name,address,telephone,rometype,romeprice) values("+'"'+h[i].name+'"'+","+'"'+h[i].address+'"'+","+'"'+h[i].telephone+'"'+","+'"'+h[i].rometype+'"'+","+h[i].romeprice+")";


int j = db.updateMessage(sql_insert);
System.out.println(j);
}


String sql_select = "select * from hotel";
ArrayList<Hotel> x = db.getMessage(sql_select);
for(Hotel temp : x) {
System.out.println(temp);
}


String sql_del = "delete from hotel where name = "+'"'+"asd"+'"';
int i = db.updateMessage(sql_del);
System.out.println(i);
db.DBClose();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值