An example of oracle and mysql connection in java, including jar

1. I have uploaded the required jar package, and the required can be downloaded

/*
* database connection
*/



public static void conn_DB() {
try {
System.out.println("Connecting to the database....... ");
//Connect to mysql
// Class.forName("com.mysql.jdbc.Driver");//Load mysql driver class
// String url = "jdbc:mysql://localhost:3306/tzwljgcp"; // The url is the connection string
// String user = "root";// Database user name
// String pwd = "password";// Database password

// Connection oracle
Class.forName("oracle.jdbc.driver.OracleDriver ");// Load oracle driver class
String url = "jdbc:oracle:thin:@IP:1521/orcl";// url is connection string
String user = "username";// Database username
String pwd = "password";

// database password conn = (Connection) DriverManager.getConnection(url, user, pwd);
//System.out.println("Database connection is successful!!!");
// Step 3: Create statement, each Statement object can only open one ResultSet object at the same time.
stat = conn.createStatement();
} catch (Exception e) {
//System.out.println(e.getMessage());
e.printStackTrace();
}
}

/**
*disconnect
*/
public static void closeMysql() {
try {
if (rs != null)
rs.close();
if (stat != null)
stat.close();
if (conn != null)
conn.close();
} catch (SQLException e ) {
e.printStackTrace();
//////System.out.println(e);
}
}

/**
*Query data
*/
public List select() {
List ll = new ArrayList();
try {
// get the result set
rs = stat.executeQuery("select c_url from o2o_res_plat a where c_res_plat=1 and zt is null");
// used to get about An object of type and attribute information for the columns in the ResultSet object
rsmd = rs.getMetaData();
while (rs.next()) {
int z = 0;
try {
Map rowData = new HashMap();
for (int i = 1; i <= rsmd.getColumnCount(); i++)
rowData.put(rsmd.getColumnName(i), rs.getString(i));
ll.add(rowData);
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return ll;
}

public static  void update(Map map,String type) {
String str = "update o2o_res_plat set zt=?  where c_url=?";
try {
pstat = conn.prepareStatement(str);
pstat.setString(1, type);
pstat.setString(2, map.get("c_url").toString());
int i = pstat.executeUpdate();
  //  ////System.out.println(i);
} catch (Exception e) {
// TODO: handle exception
}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326177823&siteId=291194637