JDBC连接数据库
准备jar
mysql-connector-java-5.1.0-bin.jar
Connection conn = null;
Statement stat = null;
try {
//加载驱动 方言
Class.forName("com.mysql.jdbc.Driver");
//准备数据库连接路径
String url = "jdbc:mysql://127.0.0.1:3306/xxshop";
//用户名与密码
String username = "root";
String userpwd = "root";
//根据路径,用户名,密码 使用DriverManager获取数据库connection连接
conn = DriverManager.getConnection(
url,username,userpwd);
//准备要执行的SQL语句
String sql= "select user_id,user_name from sf_user";
//创建Statement对象 用于执行SQL语句
stat = conn.createStatement();
//执行SQL语句
ResultSet rs = stat.executeQuery(sql);
//处理ResultSet结果集
//rs.next() 返回boolean 值
while(rs.next()){
Long user_id = rs.getLong(1);
String user_name = rs.getString(2);
System.out.println("用户ID:"+user_id);
System.out.println("用户名:"+user_name);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//释放资源
try {
stat.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
数据库驱动
com.microsoft.jdbc.sqlserver.SQLServerDriver(SQL Server)
com.mysql.jdbc.Driver(MySql)
oracle.jdbc.driver.OracleDriver(Oracle)
Statement常用对象
ResultSet executeQuery(sql)
执行sql查询语句,并返回ResultSet对象
int executeUpdate(sql)
执行insert,update,delete语句,返回受影响行数
boolean execute(sql)
执行insert,update,delete语句,返回true或false false成功
防止数据库乱码及日期出错
放入url连接路径之后
?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
PreparedStatement对象使用
防止SQL注入,使用占位符“?”方式进行SQL拼接
使用PreparedStatement对象进行用户登录
/**
* 定义登录方法(传入用户输入的用户名与密码)
*/
public User loginUser(String user_name,String user_pwd){
Connection conn = null;
PreparedStatement ps = null;
try {
//加载驱动 方言
Class.forName("com.mysql.jdbc.Driver");
//准备数据库连接路径
String url = "jdbc:mysql://127.0.0.1:3306/xxshop?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull";
//用户名与密码
String username = "root";
String userpwd = "root";
//获取Connection对象
conn = DriverManager.getConnection(
url,username,userpwd);
//准备登录SQL语句 使用占位符?代表参数
String sql ="select user_id,user_name from sf_user where user_name=? and user_password=?";
//预编译SQL语句
ps = conn.prepareStatement(sql);
//ps.setXXXX(位置<从1开始>,值<参数>)
ps.setString(1,user_name);
ps.setString(2,user_pwd);
//执行查询
ResultSet rs = ps.executeQuery();
//用于判断 等于null登录失败,否则成功
User user = null;
while(rs.next()){
//进入循环,登录成功,创建user对象
user = new User();
//使用rs.getXXX("返回的列名") 放入user对象
user.setUser_id(rs.getLong("user_id"));
user.setUser_name(rs.getString("user_name"));
}
return user;//返回用户登录对象
} catch (Exception e) {
e.printStackTrace();
} finally {
//释放资源
try {
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
返回List集合核心代码
List<Goods> goodsList = new ArrayList<Goods>();
while(rs.next()){
Goods goods = new Goods();
goods.setGoods_id(rs.getLong("goods_id"));
goods.setGoods_title(rs.getString("goods_title"));
goods.setGoods_introduce(rs.getString("goods_introduce"));
goodsList.add(goods);
}
return goodsList;
修改商品信息核心代码 对象入参
String sql = "update sf_goods set goods_title=?," +
"goods_browse=?,goods_state=? where goods_id=?";
ps = conn.prepareStatement(sql);
ps.setString(1,goods.getGoods_title());
ps.setInt(2,goods.getGoods_browse());
ps.setInt(3,goods.getGoods_state());
ps.setLong(4,goods.getGoods_id());
int i = ps.executeUpdate();
return i;