81,092
社区成员
发帖
与我相关
我的任务
分享
package com;
import java.sql.*;
public class JDBConnection {
private final String dbDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; //连接sql数据库的方法
private final String url = "jdbc:microsoft:sqlserver://10.28.160.8:1433;DatabaseName=dzgs";
private final String userName = "sa";
private final String password = "123";
private Connection con = null;
public JDBConnection() {
try {
Class.forName(dbDriver).newInstance(); //加载数据库驱动
} catch (Exception ex) {
System.out.println("数据库加载失败");
}
}
//创建数据库连接
public boolean creatConnection() {
try {
con = DriverManager.getConnection(url, userName, password);
con.setAutoCommit(true);
} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println("creatConnectionError!");
}
return true;
}
//对数据库的增加、修改和删除的操作
public boolean executeUpdate(String sql) {
if (con == null) {
creatConnection();
}
try {
Statement stmt = con.createStatement();
int iCount = stmt.executeUpdate(sql);
System.out.println("操作成功,所影响的记录数为" + String.valueOf(iCount));
} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println("executeUpdaterError!");
}
return true;
}
//对数据库的查询操作
public ResultSet executeQuery(String sql) {
ResultSet rs;
try {
if (con == null) {
creatConnection();
}
Statement stmt = con.createStatement();
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
System.out.println(e.getMessage());
return null;
}
} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println("executeQueryError!");
return null;
}
return rs;
}
//关闭数据库的操作
public void closeConnection() {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
System.out.println("Failed to close connection!");
} finally {
con = null;
}
}
}
}
package com;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Json{
public static void main(String[] args) throws JSONException {
JDBConnection dbc = null;
dbc = new JDBConnection();
// 组Json串
String json = "{\"name\":\"reiz\"}";
// 根据Json串生成Json对象
JSONObject jsonObj = new JSONObject(json);
// 获得name键对应的值.
String name = jsonObj.getString("name");
System.out.println(name) ; // 输出 reiz
// 向Json中追加一个R字符串.initial:R
jsonObj.put("initial", name.substring(0, 1).toUpperCase());
// 向Json对象中加入对象类型数据(map)
Map<String, String> ingredients = new HashMap<String, String>();
ingredients.put("apples", "3kg");
ingredients.put("sugar", "1kg");
ingredients.put("pastry", "2.4kg");
ingredients.put("bestEaten", "outdoors");
jsonObj.put("ingredients", ingredients);
// 获得Map对象的字符串
String ingre = jsonObj.getString("ingredients");
System.out.println(ingre) ;
// 输出 {"apples":"3kg","sugar":"1kg","bestEaten":"outdoors","pastry":"2.4kg"}
// 把Map对象生成Json对象
JSONObject jsonIngre = new JSONObject(ingre);
System.out.println(jsonIngre.getString("apples")) ; // 输出 3kg
System.out.println(jsonObj);
// 输出 {"ingredients":{"apples":"3kg","sugar":"1kg","bestEaten":"outdoors","pastry":"2.4kg"},"initial":"R","name":"reiz"}
}
}