黑马程序员技术交流社区
标题:
JDBC 基础改进型 代码
[打印本页]
作者:
来明坤
时间:
2012-9-5 21:56
标题:
JDBC 基础改进型 代码
package info.dyndns.oszc.Introduce;
import java.sql.*;
public class BasePlus {
public static void main(String[] args) throws Exception {
template();
}
public static void template() throws Exception {
String driver = "com.mysql.jdbc.Driver";
Connection conn = null;
Statement st = null;
ResultSet rs = null;
String user = "oszc";
String password = "1234";
String url = "jdbc:mysql://localhost:3306/comment";
String sql = "select * from say";
try {
Class.forName(driver);
conn = DriverManager.getConnection(url,user,password);
st = conn.createStatement();
rs = st.executeQuery(sql);
while (rs.next()){
System.out.println(rs.getObject(1)+"\t"+rs.getObject(2)+"\t"+rs.getObject(3));
}
}finally{
try{
rs.close();
}
finally{
try{
st.close();
}
finally{
conn.close();
}
}
}
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2