使用教程::http://www.yiibai.com/jdbc/statement-object-example.html
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnectMysql
{
public static void main(String[] args)
{
conectMysql();
}
public static void conectMysql()
{
//加载jdbc驱动类
Connection conn = null;
ResultSet rs = null;
try
{
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取数据库的连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/huawei","root","passwd");
//得到代表SQL语句的对象
Statement stmt = conn.createStatement();
//执行sql语句
rs = stmt.executeQuery("SELECT name,id,score FROM stu_tbl");
//把数据打印出来
while (rs.next())
{
System.out.println(rs.getObject("name"));
System.out.println(rs.getObject("id"));
System.out.println(rs.getObject("score"));
}
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
//把这两个流关闭
conn.close();
rs.close();
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("conn的具体类型是:"+conn.getClass().getName());
}
}