Home >Database >Mysql Tutorial >jdk连接mysql测试_MySQL
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