上个学期做了一个简易版的日程管理,主要用android+j2ee+mysql实现,发现时间一久,很多东西都记不住,所以在这里记录一下
首先是一些连接数据库的操作
第一:在lib下放入驱动
接下来看一下一些增删改查的操作:虽然都差不多,但是还是都贴出代码吧
查询:
String sql ="select * from users where name=? and password=?";
PreparedStatement pstmt = null;
Connection con;
//驱动程序名
String driver = "com.mysql.jdbc.Driver";
//URL指向要访问的数据库名mydata
String url = "jdbc:mysql://localhost:3306/person";
//MySQL配置时的用户名
// String user = "root";
//MySQL配置时的密码
String password = "123456";
try {
//加载驱动程序
Class.forName(driver);
//1.getConnection()方法,连接MySQL数据库!!
con = DriverManager.getConnection(url,"root",password);
pstmt = con.prepareStatement(sql);
pstmt.setString(1, user);
pstmt.setString(2, psw);
System.out.println("query the table");
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
System.out.println("query from users success ");
Users u = new Users();
u.setUserName(user);
u.setUserPassword(psw);
return 1;
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
增加:
public static int add(String userId,String contentId,String detail,String date,String time){
String sql = "insert into schedule(id,userId,detail,date,time) values('"+
contentId+"','"+userId+"','"+detail+"','"+date+"','"+time+"')";
Connection con;
//驱动程序名
String driver = "com.mysql.jdbc.Driver";
//URL指向要访问的数据库名mydata
String url = "jdbc:mysql://localhost:3306/person";
//MySQL配置时的用户名
// String user = "root";
//MySQL配置时的密码
String password = "123456";
//遍历查询结果集
try {
//加载驱动程序
Class.forName(driver);
//1.getConnection()方法,连接MySQL数据库!!
con = DriverManager.getConnection(url,"root",password);
java.sql.Statement stmt = con.createStatement();
stmt.executeUpdate(sql);
} catch(ClassNotFoundException e) {
//数据库驱动类异常处理
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
//数据库连接失败异常处理
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
}
return 1;
}
删除:
public static int delete(String userId,String contentId){
String sql = "delete from schedule where userId="+userId+" and id="+contentId;
Connection con;
//驱动程序名
String driver = "com.mysql.jdbc.Driver";
//URL指向要访问的数据库名mydata
String url = "jdbc:mysql://localhost:3306/person";
//MySQL配置时的用户名
// String user = "root";
//MySQL配置时的密码
String password = "123456";
//遍历查询结果集
try {
//加载驱动程序
Class.forName(driver);
//1.getConnection()方法,连接MySQL数据库!!
con = DriverManager.getConnection(url,"root",password);
java.sql.Statement stmt = con.createStatement();
stmt.executeUpdate(sql);
return 1;
} catch(ClassNotFoundException e) {
//数据库驱动类异常处理
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
//数据库连接失败异常处理
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
}
return 0;
}
而其他的一些与客户端的数据的传输则是通过http完成,主要在servlet文件里面的doPost方法进行操作,通过request获得客户端传来的信息,再通过response返回。
不过要注意编码问题
response.setCharacterEncoding("GBK");
response.setContentType("text/html");
通过printWriter返回数据
PrintWriter out = response.getWriter();
out.println("Server get");
通过request获取数据
String username = request.getParameter("username");