/** * 单独的java程序连接mysql数据库
* author:JavaAlpha
* date :2011-3-31 20:48:28
* IDE:EmEditor Version 9.16
*/
- import java.sql.*;
- public class Test {
- public static void main(String[] args){
- String driver = "com.mysql.jdbc.Driver";
- String url = "jdbc:mysql://localhost:3306/rftl";
- String user = "admin";
- String password = "admin";
- try {
- Class.forName(driver);
- Connection conn = DriverManager.getConnection(url, user, password);
- if(!conn.isClosed())
- System.out.println("Succeeded connecting to the Database!");
- Statement statement = conn.createStatement();
- String sql = "select * from news order by id desc";
- ResultSet rs = statement.executeQuery(sql);
- String name = null;
- while(rs.next()) {
- name = rs.getString("title");
- System.out.println(rs.getString("id")+"\t"+rs.getString("content") + "\t" + name + "\t" + rs.getString("title")+"\t"+rs.getString("time"));
- }
- rs.close();
- conn.close();
- } catch(ClassNotFoundException e) {
- System.out.println("Sorry,can`t find the Driver!");
- e.printStackTrace();
- } catch(SQLException e) {
- e.printStackTrace();
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- }
阅读(690) | 评论(0) | 转发(0) |