好久沒好好凈下心來寫代碼了,習(xí)慣了ctrl+c,ctrl+v,發(fā)現(xiàn)現(xiàn)在連最簡單的jdbc連接都忘記怎么寫了,感謝beansoft的奉獻(xiàn),借著熟悉MyEclipse下的開發(fā),好好寫寫代碼.就這么個(gè)簡單的例子,也是寫寫停停的.看來還要勤加練習(xí)啊.
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)
評(píng)論(0) 編輯 收藏 所屬分類:
java學(xué)習(xí)