好久沒好好凈下心來寫代碼了,習慣了ctrl+c,ctrl+v,發現現在連最簡單的jdbc連接都忘記怎么寫了,感謝beansoft的奉獻,借著熟悉MyEclipse下的開發,好好寫寫代碼.就這么個簡單的例子,也是寫寫停停的.看來還要勤加練習啊.
package biz;

import java.sql.*;


public class JDBCHelloWorld{

public static void main(String[] args){

try{
Class.forName("com.mysql.jdbc.Driver");

}catch(ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;

try{
conn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&charcterEnconding=GBK","root",null);
stmt = conn.createStatement();
stmt.executeUpdate("insert into student(username,password,age) values('王五','1234',25)");
rs = stmt.executeQuery("select id,username,password,age from student");

while(rs.next()){
System.out.println("姓名=" + rs.getString("username"));
System.out.println("密碼=" + rs.getString("password"));
System.out.println("年齡=" + rs.getString("age"));
}

}catch(SQLException e){
e.printStackTrace();

}finally{

try{
rs.close();

}catch(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try{
stmt.close();

}catch(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try{
conn.close();

}catch(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

posted on 2008-05-14 16:30
hurray 閱讀(740)
評論(0) 編輯 收藏 所屬分類:
java學習