文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Java查询MySQL数据库示例

Java查询MySQL数据库示例

时间:2010-05-31  来源:meteorm

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class MySQLTest {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Connection conn = null;
        try
        {
            String userName = "username";
            String password = "password";
            String url = "jdbc:mysql://localhost/test";
            Class.forName ("com.mysql.jdbc.Driver").newInstance ();
            conn = DriverManager.getConnection (url, userName, password);
            System.out.println ("Database connection established");
            PreparedStatement pstmt;
            ResultSet rset;
            pstmt = conn.prepareStatement("select count(*) from testtable");
            rset=pstmt.executeQuery();
            while (rset.next()){
                System.out.println (rset.getString(1));   // Print col 1
            }
        }
        catch (Exception e)
        {
            System.err.println ("Cannot connect to database server");
        }
        finally
        {
            if (conn != null)
            {
                try
                {
                    conn.close ();
                    System.out.println ("Database connection terminated");
                }
                catch (Exception e) { /* ignore close errors */ }
            }
        }

 }
}
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载