1.连接mysql数据库
package com.geminno.jdbc.day01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MyJdbc02 {
public static void main(String[] args) {
String url="jdbc:mysql://127.0.0.1:3306/stumanage";
String username="root";
String password="1234";
Connection conn=null;
Statement tm=null;
ResultSet rs=null;
try {
//1.加载数据库的驱动
Class.forName("com.mysql.jdbc.Driver");
//2.建立连接
conn=DriverManager.getConnection(url,username,password);
//3.获取statemnet对象
tm=conn.createStatement();
//4.发送执行sql语句
String sql="select * from score";
rs=tm.executeQuery(sql);
//5.处理结果集,显示结果
while(rs.next()) {
System.out.println(rs.getString("sno")+"--"+rs.getString("cno")+"--"+rs.getDouble("degree"));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(tm!=null) {
try {
tm.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
}
}
}