bitsCN.com
import java.sql.*;
public class testjdbc {
public static void main(string[] arges) throws sqlexception,classnotfoundexception{
string user="root";
string pwd="mysql";
//设定url
string myjdbc="jdbc:mysql://localhost:3306/test";
//加载mysql驱动
class.forname("com.mysql.jdbc.driver");
//创建连接会话
connection myconnection=drivermanager.getconnection(myjdbc,user,pwd);
//创建语句块对象
statement myoperation=myconnection.createstatement();
//获取结果集对象
resultset record=myoperation.executequery("select *from test");
while(record.next()){
//输出结果集对象
system.out.println(record.getint("id")+","+record.getstring("name"));
}
//关闭结果集
try{
if(record!=null)
//关闭结果集时需要处理异常
record.close();
}catch(exception e){
e.printstacktrace();
}finally{
try{
if(myconnection!=null)
myconnection.close();
}catch(exception e){
e.printstacktrace();
}
}
}
}
本文出自 “java技术社区” 博客
bitsCN.com
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号