1.Java a. Database connection 1.Drivermanager link database String className, url, uid, pwd; className = \ ‘Oracle.jdbc.driver.OracleDriver \’; uid = \ ‘scott \’; pwd = \ ‘ tiger \’;…
1.Java
a. 1.Drivermanager link database database connection
String className, url, uid, pwd;
className = \ “Oracle.jdbc.driver.OracleDriver \”;
uid = \ “scott \”;
pwd = \ “tiger \”;
url = \ “jdbc: oracle: thin: @ localhost: 1521: ora92 \”;
Class.forName (classname);
Connection conn = DriverManager.getConnection (url, uid, pwd);
2.JNDI Links Database
String jndi = \ “jdbc / db \”; / / e20-040 9L0-609 data source name
/ / Context is a set of binding names to objects composed of
Hashtable env = new Hashtable ();
Context ctx = (Context) new InitialContext.lookup (\ “env \ “);// context where access to the data source object point
DataSource ds = (DataSource) ctx.lookup (jndi); / / find the data source
Connection conn = ds.getConnection ();//
b. implementation of the sql statement
String sql;
StateMent stat = conn.createStatement ();
ResultSet rs = stat.executeQuery (sql); / / implementation of the data query (select);
stat.executeUpdate (sql); / / implementation of data update statements (inset into, delete, update, drop)
stat.close ();
c. to execute sql statements with preparedStatement
String sql = \ “inset into table (id, name) values (?,?) \”;
PreparedStatement ps = conn.prepareStatement (sql);
ps.setInt (1,001);
ps.setString (2, \ “zhangmanli \”);
ps.executeQuery ();
int count = ps.executeUpdate ();
d. address the implementation of the results
Query, ResultSet object to return recordset
Update statement, return number, said the impact of the number of records updated
javax.sql .*
javax.naming .*;
Data processing:
A close connection to automatically submit
conn.setAutoCommit (false);
2 implementation of a series of sql statements
Statement sm;
sm = conn.createStatement (sql);
sm.executeUpdate ();
sm.close ();
3. Submit:
conn.commit ();
4. Rollback mechanism;
conn.rollback ();
e: Threading:
D: jndi and dataSource to get the database link:
import java.sql.ResultSet;
import java.sql .*;
import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Hashtable;
import java.util.Properties;
import java.io. *;
public class BasicExample (
public static void main (String args []) (
Connection conn = null;
try (
Properties prop = new Properties ();
prop.load (new FileInputStream (\ “simple.properties \”));
Hashtable env = new Hashtable ();
env.put (Context.INITIAL_CONTEXT_FACTORY, prop.getProperty (\ “INITIAL_CONTEXT_FACTORY \”));
env.put (Context.PROVIDER_URL, prop.getProperty (\ “PROVIDER_URL \”));
InitialContext ctx = new InitialContext (env);
DataSource ds = (DataSource) ctx.lookup (\ “Book \”);
Conn = ds.getConnection ();
Statement stat = conn.createStatement ();;
ResultSet rs = stmt.executeQuery (sql);
while (rs.next ()) (
int id = Integer.parseInt (rs.getString (\ “userId \”));
String userName = rs.getString (\ “username \”);
)
) Catch (SQLException e) (
e.printStackTrace ();
) Finally (
try (
if (conn! = null) (
conn.close ();
)
) Catch (SQLException e) (
e.printStackTrace ();
)
)
)
);
public class DateUtils (/ ** * get the current time and date string * / public static String getCurrentDateStr (DateFormatType dateFormatType) (Date date = getCurrentDat …
public class DateUtils (
/ **
* Get the current time and date string
* /
public static String getCurrentDateStr (DateFormatType dateFormatType) (
Date date = getCurrentDate ();
return (String) OpearationDate (date, dateFormatType.getValue ());
)
/ **
* Time, date format string
* /
public static String formatDate (Date date, DateFormatType dateFormatType) (
return (String) OpearationDate (date, dateFormatType.getValue ());
)
/ **
* From the string parsing into the time, date
* /
public static Date parseDate (String dateStr, DateFormatType dateFormatType) (
return (Date) OpearationDate (dateStr, dateFormatType.getValue ());
)
/ **
* Get the current system time (original format)
* /
public static Date getCurrentDate () (
Date date = new Date (System.currentTimeMillis ());
return date;
)
/ **
* Get the current date of the year, month, day, hour, minute, second
* /
public static int getCurrentTime (TimeFormatType timeFormatType) (
return getTime (getCurrentDate (), timeFormatType);
)
/ **
* Get the specified date of the year, month, day, hour, minute, second
* /
public static int getTime (Date date, TimeFormatType timeFormatType) (
try (
Calendar c = Calendar.getInstance ();
c.setTime (date);
int type = timeFormatType.getValue ();
int i = c.get (type);
return type == 2? i + 1: i;
) Catch (Exception e) (
throw new RuntimeException (“get failed”, e);
)
)
/ **
* Get the number of milliseconds specified date
* /
public static long getMillis (Date date) (
java.util.Calendar c = java.util.Calendar.getInstance ();
c.setTime (date);
return c.getTimeInMillis ();
)
/ **
* Date added together, by operation
*
* Results are returned units: days
* /
public static int operationDate (Date date, Date diffDate, DateOperationType dateOperationType) (
long add = getMillis (date) + getMillis (diffDate);
long diff = getMillis (date) – getMillis (diffDate);
return (int) ((dateOperationType.getValue ()? add: diff) / (24 * 3600 * 1000));
)
/ **
* Add date month, reducing operation
* /
public static Date operationDateOfMonth (Date date, int month, DateOperationType dateOperationType) (
Calendar c = Calendar.getInstance ();
c.setTime (date);
c.add (Calendar.MONTH, dateOperationType.getValue ()? month: month – (month * 2));
return c.getTime ();
)
/ **
* Date of days add up, by operation
* /
public static Date operationDateOfDay (Date date, int day, DateOperationType dateOperationType) (
Calendar c = Calendar.getInstance ();
c.setTime (date);
long millis = c.getTimeInMillis ();
long millisOfday = day * 24 * 3600 * 1000;
long sumMillis = dateOperationType.getValue ()? (millis + millisOfday): (millis – millisOfday);
c.setTimeInMillis (sumMillis);
return c.getTime ();
)
private static Object OpearationDate (Object object, String formatStr) (
if (object == null | | null == formatStr | | “”. equals (formatStr)) (
throw new RuntimeException (“argument can not be empty”);
)
SimpleDateFormat format = new SimpleDateFormat (formatStr);
try (
if (object instanceof Date)
return format.format (object);
else
return format.parse (object.toString ());
) Catch (Exception e) (
throw new RuntimeException (“operation failed”, e);
)
)
public enum DateOperationType (
/ **
* Add operation
* /
ADD (true),
/ **
* Reduction operation
* /
DIFF (false);
private final boolean value;
DateOperationType (boolean operation) (
this.value = operation;
)
public boolean getValue () (
return value;
)
)
public enum TimeFormatType (
YEAR (1), MONTH (2), DAY (5), HOUR (11), MINUTE (12), SECOND (13);
private final int value;
TimeFormatType (int formatStr) (
this.value = formatStr;
)
public int getValue () (
return value;
)
)
public enum DateFormatType (
/ **
* Format: yyyy-MM-dd HH: mm: ss
* /
DATE_FORMAT_STR (“yyyy-MM-dd HH: mm: ss”),
/ **
* Format: yyyyMMddHHmmss
* /
SIMPLE_DATE_TIME_FORMAT_STR (“yyyyMMddHHmmss”),
/ **
* Format: yyyy-MM-dd
* /
SIMPLE_DATE_FORMAT_STR (“yyyy-MM-dd”),
/ **
* Format: yyyy / MM / dd
* /
SIMPLE_DATE_FORMAT_VIRGULE_STR (“yyyy / MM / dd”),
/ **
* Format: HH: mm: ss
* /
HOUR_MINUTE_SECOND (“HH: mm: ss”),
/ **
* Format: HH: mm
* /
HOUR_MINUTE (“HH: mm”);
private final String value;
DateFormatType (String formatStr) (
this.value = formatStr;
)
public String getValue () (
return value;
)
)
)