好久没好好净下心来写代码了,习惯了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 阅读(736)
评论(0) 编辑 收藏 所属分类:
java学习