11.27
今天继续写那个web系统,没遇道什么问题,明年计划继续写这个web系统
代码部分:
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import bean.TestBean;
import util.DBUtil;
public class ChangeDao {
public static TestBean getByName(String name) {
String sql = "select * from test1205 where name ="+name;
Connection conn = DBUtil.getConnection();
Statement state = null;
ResultSet rs = null;
TestBean test = new TestBean();
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
String name1 = rs.getString("name");
test.setName(name1);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return test;
}
public static boolean changeTheName(String name,String name1) {
Connection conn = DBUtil.getConnection();
boolean flag = false;
try {
String sql ="update test1205 set name = '"+name1+"' where name = '"+name+"'";
System.out.println(sql);
PreparedStatement pstmt = conn.prepareStatement(sql);
int i = pstmt.executeUpdate();
pstmt.close();
conn.close();
if(i>0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return flag;
}
}
package dao;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import util.DBUtil;
public class DeleteDao {
public static boolean deleteTheName (String name) {
boolean f = false;
String sql = "delete from test1205 where name='" + name + "'";
//select from test1205 where name = '1'
Connection conn = DBUtil.getConnection();
Statement state = null;
int a = 0;
try {
state = conn.createStatement();
a = state.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(state, conn);
}
if (a > 0) {
f = true;
}
return f;
}
}
package dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import bean.TestBean;
import util.DBUtil;
public class SelectDao {
public static List<TestBean> list() {
String sql = "select * from test1205";
List<TestBean> test = new ArrayList<>();
Connection conn = DBUtil.getConnection();
Statement state = null;
ResultSet rs = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while (rs.next()) {
TestBean test1 = new TestBean();
String name = rs.getString("name");
test1.setName(name);
test.add(test1);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(rs, state, conn);
}
return test;
}
}