7.12问题集
For-Each循环
Java[編輯]
Java語言從JDK 1.5.0開始引入foreach循環。
以下代碼用於循環列印myArray數組中的每個元素,java中的foreach循環使用for關鍵字,而非foreach。
for (int x : myArray){
System.out.println(x);
}
他的语法如下
for(type element: array)
{
System.out.println(element);
}
两种方式实现插入
第一种直接书写sql语句。
public void insert(UserInfo userInfo){
Connection conn=BaseConnection.getConnection();
PreparedStatement ps=null;
try {
String sqlinsert="insert into userinfo(name,password)"+ "values('"+userInfo.getName()+"','"+userInfo.getPassword()+"')";
System.out.println(sqlinsert);
ps=(PreparedStatement) conn.prepareStatement(sqlinsert);
int i=ps.executeUpdate();
if (i>0) {
System.out.println("添加成功");
}else {
System.out.println("添加失败");
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if (ps!=null) {
ps.close();
}
if (conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
第二种使用占位符书写
public void insert2(UserInfo userInfo){
Connection conn=BaseConnection.getConnection();
PreparedStatement ps=null;
try {
String sqlinsert="insert into userinfo(name,password)"+ "values(?,?)";
System.out.println(sqlinsert);
ps=(PreparedStatement) conn.prepareStatement(sqlinsert);
ps.setString(1, userInfo.getName());
ps.setString(2, userInfo.getPassword());
int i=ps.executeUpdate();
if (i>0) {
System.out.println("添加成功");
}else {
System.out.println("添加失败");
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if (ps!=null) {
ps.close();
}
if (conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
他的调用我也补充一下
public static void main(String[] args) {
ArrayList<UserInfo> arrayList= new UserInfoDao().getList();
UserInfo userInfo=new UserInfo();
userInfo.setName("shan");
userInfo.setPassword("123---");
new UserInfoDao().insert(userInfo);
}
}
更新操作
public void update(UserInfo userInfo){
Connection conn=BaseConnection.getConnection();
PreparedStatement ps=null;
String updateSQL="update userinfo set name=?,password=?"+"where id=?";
try {
ps=(PreparedStatement) conn.prepareStatement(updateSQL);
ps.setString(1, userInfo.getName());
ps.setString(2, userInfo.getPassword());
ps.setInt(3, userInfo.getId());
System.out.println(updateSQL);
int a=ps.executeUpdate();
if (a>0) {
System.out.println("更改完成");
}
if (a<0) {
System.out.println("更改失败");
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if (ps!=null) {
ps.close();
}
if (conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
注意点:ps.setString(1, userInfo.getName());这是从1开始添加。
删除数据
public void delete(int id) {
Connection conn=BaseConnection.getConnection();
PreparedStatement ps=null;
String deleteSQL="delete from userinfo where id=?";
try {
ps=(PreparedStatement) conn.prepareStatement(deleteSQL);
ps.setInt(1, id);
int a=ps.executeUpdate();
if (a>0) {
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if (ps!=null) {
ps.close();
}
if (conn!=null) {
conn.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
进行代码优化——1,关闭资源
注意:参数有两个或者三个的。这时候就要使用参数多态性
当使用的时候就直接调用者两个函数就可以了
public static void closeResource(PreparedStatement ps,Connection conn){
try {
if (ps!=null) {
ps.close();
}
if (conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void closeResource(ResultSet rs,PreparedStatement ps,Connection conn){
try {
if (rs!=null) {
rs.close();
}
if (ps!=null) {
ps.close();
}
if (conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
代码优化之二———使用boolean值作为返回值来判断是否修改成功
前面的修改例子中,修改成功是显示在控制台上。而这种方法修改成功是显示在返回值
public boolean delete(int id) {
boolean b=false;
Connection conn=BaseConnection.getConnection();
PreparedStatement ps=null;
String deleteSQL="delete from userinfo where id=?";
try {
ps=(PreparedStatement) conn.prepareStatement(deleteSQL);
ps.setInt(1, id);
int a=ps.executeUpdate();
if (a>0) {
b=true;
}else {
System.out.println("删除失败");
}
} catch (Exception e) {
e.printStackTrace();
}finally{
BaseConnection.closeResource(ps, conn);
}
return b;
}
完整的JDBC代码
package com.zsz.develop.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import org.omg.CORBA.portable.ValueBase;
import org.omg.PortableServer.ID_ASSIGNMENT_POLICY_ID;
import com.mysql.jdbc.PreparedStatement;
import com.zsz.develop.bean.UserInfo;
import com.zsz.develop.util.BaseConnection;
public class UserInfoDao {
public ArrayList<UserInfo> getList(){
ArrayList<UserInfo> arrayList=new ArrayList<UserInfo>();
Connection conn=BaseConnection.getConnection();
PreparedStatement ps=null;
ResultSet rs=null;
String sql="select * from userinfo";
try {
ps=(PreparedStatement) conn.prepareStatement(sql);
rs =ps.executeQuery();
while (rs.next()) {
UserInfo userInfo=new UserInfo();
userInfo.setId(rs.getInt("id"));
userInfo.setName(rs.getString("name"));
userInfo.setPassword(rs.getString("password"));
arrayList.add(userInfo);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
BaseConnection.closeResource(rs, ps, conn);
}
return arrayList;
}
public boolean insert(UserInfo userInfo){
boolean b=false;
Connection conn=BaseConnection.getConnection();
PreparedStatement ps=null;
try {
String sqlinsert="insert into userinfo(name,password)"+ "values('"+userInfo.getName()+"','"+userInfo.getPassword()+"')";
System.out.println(sqlinsert);
ps=(PreparedStatement) conn.prepareStatement(sqlinsert);
int i=ps.executeUpdate();
if (i>0) {
b=true;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
BaseConnection.closeResource( ps, conn);
}
return b;
}
public boolean insert2(UserInfo userInfo){
boolean b=false;
Connection conn=BaseConnection.getConnection();
PreparedStatement ps=null;
try {
String sqlinsert="insert into userinfo(name,password)"+ "values(?,?)";
System.out.println(sqlinsert);
ps=(PreparedStatement) conn.prepareStatement(sqlinsert);
ps.setString(1, userInfo.getName());
ps.setString(2, userInfo.getPassword());
int i=ps.executeUpdate();
if (i>0) {
b=true;
}else {
System.out.println("添加失败");
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
BaseConnection.closeResource(ps, conn);
}
return b;
}
public boolean update(UserInfo userInfo){
boolean b=false;
Connection conn=BaseConnection.getConnection();
PreparedStatement ps=null;
String updateSQL="update userinfo set name=?,password=?"+"where id=?";
try {
ps=(PreparedStatement) conn.prepareStatement(updateSQL);
ps.setString(1, userInfo.getName());
ps.setString(2, userInfo.getPassword());
ps.setInt(3, userInfo.getId());
System.out.println(updateSQL);
int a=ps.executeUpdate();
if (a>0) {
b=true;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
BaseConnection.closeResource(ps, conn);
}
return b;
}
public boolean delete(int id) {
boolean b=false;
Connection conn=BaseConnection.getConnection();
PreparedStatement ps=null;
String deleteSQL="delete from userinfo where id=?";
try {
ps=(PreparedStatement) conn.prepareStatement(deleteSQL);
ps.setInt(1, id);
int a=ps.executeUpdate();
if (a>0) {
b=true;
}else {
System.out.println("删除失败");
}
} catch (Exception e) {
e.printStackTrace();
}finally{
BaseConnection.closeResource(ps, conn);
}
return b;
}
}