到今天我才感觉我是学到了,也把我学的连起来了!
今天呢一天早上都在练数据库和myeclipse的连接以及Tomcat连接(安装这几个真的烦人,特别是MySQL),并进行简单的增删改查,不逼逼上代码。
实现代码:
package com.chiansoft.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.mail.Flags.Flag;
import com.chiansoft.units.JDBCUtil;
import com.chinasoft.bean.user;
public class RegistDao {
public boolean registUser(String username,String password)
{
int flag =0;
Connection connection = JDBCUtil.getConnection();
String sql="insert into user2 values(null,?,?,?)";
PreparedStatement preparedStatement;
try {
preparedStatement =connection.prepareStatement(sql);
preparedStatement.setString(1, username);
preparedStatement.setString(2, password);
preparedStatement.setString(3, "wwwwww");
flag=preparedStatement.executeUpdate();
System.out.print(flag );
return true;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return false;
}
}
public user querryUser(String username,String password)
{
user er=new user();
String sql="select * from user2 where username=? and password =?" ;
Connection connection = JDBCUtil.getConnection();
java.sql.PreparedStatement preparedStatement;
ResultSet resultSet;
try {
preparedStatement =connection.prepareStatement(sql);
preparedStatement.setString(1, username);
preparedStatement.setString(2, password);
resultSet=preparedStatement.executeQuery();
while (resultSet.next()) {
String id=resultSet.getString(1);
String qureeusername=resultSet.getString(2);
String qureetoken=resultSet.getString(4);
String qureepassword =resultSet.getString(3);
er.setId(id);
er.setPassword(qureepassword);
er.setUsername(qureeusername);
er.setToken(qureetoken);
er.setCode(200);
}
} catch (Exception e) {
// TODO: handle exception
er.setCode(100);
e.printStackTrace();
}
if (er.getCode()==0){
er.setCode(100);
}
return er;
}
public boolean resetPassword (String username,String oldpassword ,String newpassword)
{
int flag =0;
Connection connection = JDBCUtil.getConnection();
String sql="update user2 set password=?where username =? and password =?";
java.sql.PreparedStatement preparedStatement;
try {
preparedStatement =connection.prepareStatement(sql);
preparedStatement.setString(1, newpassword);
preparedStatement.setString(2, username);
preparedStatement.setString(3,oldpassword);
flag=preparedStatement.executeUpdate();
return true;
} catch (Exception e) {
// TODO: handle exception
flag=0;
e.printStackTrace();
return false;
}
}
public boolean delete ( String username,String password)
{
int flag =0;
Connection connection = JDBCUtil.getConnection();
String sql="delete from user2 where username=? and password=?";
PreparedStatement preparedStatement;
try {
preparedStatement =connection.prepareStatement(sql);
preparedStatement.setString(1, username);
preparedStatement.setString(2, password);
flag=preparedStatement.executeUpdate();
System.out.print(flag );
return true;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return false;
}
}
}
测试代码:(就放一张吧,其他的都是改过来的)
删除的servlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
RegistDao registDao =new RegistDao();
String username=request.getParameter("username");
String password=request.getParameter("password");
boolean isregist= registDao.delete(username, password);
user er =new user();
out.print(isregist);
if (isregist)
{
er.setCode(200);
er.setUsername(username);
}
else
{
er.setCode(100);
er.setUsername(username);
}
out.print(JSONObject.fromObject(er ));
}
好了这是早上的精华,下午就都是安装Android studio 安装教程奉上
AMD真的苦逼,连Android studio的虚拟机都不能用
(https://blog.csdn.net/dayexiaofan/article/details/82686192?utm_source=app)