mysql的時間字段類型為char(20),
創(chuàng)新互聯(lián)主要從事成都做網(wǎng)站、成都網(wǎng)站制作、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)都江堰,十年網(wǎng)站建設(shè)經(jīng)驗(yàn),價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220
String sql = "select t.* from t_book t where insert_date to_date( ' "+timeStr + " ', 'yyyy-mm-dd hh:mi:ss ') ";
這段語句我該成
String sql = "select t.* from t_book t where insert_date to_date( ' "+timeStr + " ', 'yyyy-mm-dd hh:mi:ss ') ";
public class DBConn {
private static final String DRIVER = "com.mysql.jdbc.Driver"; //定義數(shù)據(jù)庫驅(qū)動
private static final String URL = "jdbc:mysql://127.0.0.1:3306/words"; //數(shù)據(jù)庫所在位置,127.0.0.1代表本機(jī),3306是數(shù)據(jù)庫端口,words是數(shù)據(jù)庫名
private static final String USERNAME = "root"; //用戶名
private static final String PASSWORD = "root";//密碼
private static Connection conn; //定義一個變量:等會用來創(chuàng)建數(shù)據(jù)庫連接
private Statement stmt; //定義一個 statement對象,等會用來執(zhí)行SQL語句用的
private ResultSet rs; //定義結(jié)果集,等會用來存放查詢結(jié)果
public DBConn() {//構(gòu)造方法
}
public static synchronized Connection getConn() {
try {//獲取數(shù)據(jù)庫連接,調(diào)用該方法即可獲得一個數(shù)據(jù)庫連接
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
return conn;//返回數(shù)據(jù)庫連接
}
public void init(Connection conn) {
try {//init()方法需要傳入一個 conn 連接做參數(shù),然后用這個conn連接創(chuàng)建statement對象,調(diào)用該方法即可拿到一個statement對象來使用。
stmt = conn.createStatement();
} catch (Exception e) {
}
}
public void close() {
try {//執(zhí)行完所有操作后,調(diào)用該方法來關(guān)閉數(shù)據(jù)庫連接
if (rs != null)
rs.close();
} catch (Exception e) {
}
try {
if (stmt != null)
stmt.close();
} catch (Exception e) {
}
try {
if (conn != null)
conn.close();
} catch (Exception e) {
}
}
public int select(String sql) {//輸入一個 sql 字符串,即查詢語句
int k = -10;
init(getConn()); //調(diào)用init()方法可以獲得一個statement對象,調(diào)用getConn可以獲得一個數(shù)據(jù)庫連接
k = 0;
rs = stmt.executeQuery(sql); //執(zhí)行查詢語句,把結(jié)果放到rs結(jié)果集里
if (rs.next())//遍歷RS結(jié)果集
k++;//用K來統(tǒng)計(jì)RS記錄條數(shù),貌似有點(diǎn)多余……
} catch (Exception e) {
k = -1;
close();//調(diào)用關(guān)閉數(shù)據(jù)庫連接方法
}
close();
return k;
}
public int update(String sql) {//執(zhí)行更新 sql 語句
int k = -10;
init(getConn());
try {
k = 0;
k = stmt.executeUpdate(sql);
} catch (Exception e) {
k = -1;
}
close();
return k;
}
public static void main(String[] args)throws Exception {
DBConn db = new DBConn();
/**
* ???????????????
*/
System.out.println(db.getConn().getCatalog());
}
public static String getDate() {
return new SimpleDateFormat("yyyy-MM-dd hh-mm-ss").format(Calendar
.getInstance().getTime());
}
public String chStr(String str) {
String temp2;
byte temp[] = null;
try {
temp = str.getBytes("ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
temp2 = new String(temp);
return temp2;
}
public ResultSet query(String sql) {
init(getConn());
try {
rs = stmt.executeQuery(sql);
} catch (Exception e) {
}
return rs;
}
}
1.生成java代碼
首先選擇你要生成代碼的類圖,然后單擊菜單欄的Language菜單,選擇Generate java code選項(xiàng)。
會出現(xiàn)上圖所示的對話框,選擇文件輸出目錄,單擊確定就可以了。
2. 生成MySQL語句
首先要在物理數(shù)據(jù)模型視圖下(physical data model)選擇菜單欄的Database菜單下的Generate Database,會出現(xiàn)如下對話框:
選擇輸出目錄,你如果只是生成sql腳本,在Generation type選擇Script generation就可以了。如果你想直接連接數(shù)據(jù)庫,就選擇Direct generation。當(dāng)然你要用mysql數(shù)據(jù)庫,就必須在創(chuàng)建或者用邏輯或者概念模型生成physical data model時就要把dbms選項(xiàng)選為mysql數(shù)據(jù)庫。
給你個我以前做的例子,不完整,僅供參考。
//*************************************
public ModelAndView add(HttpServletRequest req,
HttpServletResponse res) {
String username = (String) req.getSession().getAttribute("valid_user");
if (username != null) {
Integer bookid = Integer.parseInt(req.getParameter("bookid"));
Integer number = Integer.parseInt(req.getParameter("number"));
Integer userid = getUserDAO().findByName(username).getId();
this.shopcartDAO.addUserBook(userid, bookid, number);
Shopcart[] orders = this.getShopcartDAO().findUserBook(userid);
return new ModelAndView(this.getMemberPage(), "orders", orders);
} else {
return new ModelAndView(new RedirectView(getRedirectPage()));
}
}
//*******************************************************
public void addUserBook(Integer userid,Integer bookid,Integer number) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String sql = "insert into shopcart(userid,bookid,number) "
+ "values(" + userid + "," + bookid + "," + number + ")";
Connection conn = null;
Statement stmt = null;
try
{
conn = dataSource.getConnection();
stmt = conn.createStatement();
stmt.execute(sql);
} catch(SQLException ex)
{
ex.printStackTrace();
}
finally
{
try{
stmt.close();
}
catch(SQLException e1)
{
e1.printStackTrace();
}
try{
conn.close();
}
catch(SQLException e2)
{
e2.printStackTrace();
}
}
}
js全稱是javascript看著像java,但是跟java基本上沒有半毛錢關(guān)系,轉(zhuǎn)不了