一个简单的JDBC程序_连接mysql并显示表中记录
记得引mysql的包.
package cn.eboy.jdbc;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
public class Base {
/**
* @param args
*/
public static void main(String[] args) {
try {
test();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static void test() throws SQLException{
//1.注册驱动(只做一次)
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//2.建立连接
Connection connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/a0425231133", "root", "123456");
//3.创建语句
Statement statement = (Statement) connection.createStatement();
//4.执行语句
ResultSet resultSet = (ResultSet) statement.executeQuery("select * from pv_video limit 10");
//5.处理结果
while (resultSet.next()){
System.out.println(resultSet.getString(1) + "\t" + resultSet.getObject(14) + "\t");
}
//6释放资源
resultSet.close();
statement.close();
connection.close();
}
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
public class Base {
/**
* @param args
*/
public static void main(String[] args) {
try {
test();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static void test() throws SQLException{
//1.注册驱动(只做一次)
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//2.建立连接
Connection connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/a0425231133", "root", "123456");
//3.创建语句
Statement statement = (Statement) connection.createStatement();
//4.执行语句
ResultSet resultSet = (ResultSet) statement.executeQuery("select * from pv_video limit 10");
//5.处理结果
while (resultSet.next()){
System.out.println(resultSet.getString(1) + "\t" + resultSet.getObject(14) + "\t");
}
//6释放资源
resultSet.close();
statement.close();
connection.close();
}
}
结果如下:
1 Lasko
2 制裁者,神鬼制裁,The.Punisher
3 英雄不败,Young.and.Dangerous.Part
4 坏小子2,重案梦幻组2,Bad.Boys2
5 暴力社团,Gangster.High
6 赌神续集,God.of.Gamblers2
7 坏小子,Bad.Boys
8 谈判专家,Running.Out.of.Time
9 东方不败之风云再起,Swordsman.3:.East.Is
10 绝处逢生,PTU.File:Death.Trap