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
Copyright © 2019- ovod.cn 版权所有 湘ICP备2023023988号-4
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务