1,连接数据库的简单操作,
//这是链接数据库的
Class.forName("com.mysql.jdbc.Driver");
String path ="jdbc:mysql://127.0.0.1:3306/MyServletDB";
Connectioncon =DriverManager.getConnection(path,"root","root");
PreparedStatementps = (PreparedStatement)con.createStatement();
ResultSet set =ps.executeQuery("select * from tb_user");
while(set.next()){
System.out.println("result = "+set.getInt(1));
}
2,这里需要一些jar包,就是将对象,或者是数组转换为JSON的jar包。自行百度吧,我也忘记叫啥了,哈哈
//内部类的调用,先实例化外部类
TestJson.User u1 =new TestJson().new User("liyang", 25);
TestJson.User u2 =new TestJson().new User("shuyang", 22);
//User u2 = new User("shuayng", 22);
ArrayList
l.add(u1);
l.add(u2);
System.out.println("-----"+u1.getAge());
// Json 转数组需要用到set get 方法,在类需要实现该方法
JSONObject obj = JSONObject.fromObject(u1);
System.out.println(obj);
// 数组转
JSONArray array = JSONArray.fromObject(l);
System.out.println(array);
// Map转json
Map
map.put("one",u1);
map.put("two",u2);
map.put("three",l);
JSONArray mArr= JSONArray.fromObject(map);
System.out.println(mArr);
// 输出
{"age":25,"name":"liyang"}
[{"age":25,"name":"liyang"},{"age":22,"name":"shuyang"}]
[{"one":{"age":25,"name":"liyang"},"two":{"age":22,"name":"shuyang"},"three":[{"age":25,"name":"liyang"},{"age":22,"name":"shuyang"}]}]