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 */ }
            }
        }

 }
}

 

深圳人才网 深圳招聘网 深圳人才招聘网 深圳人才大市场 

企业、个人免费注册,获取想要的 深圳 软件工程师招聘信息 月薪最低3000-8000,更有高端猎头职位! 

www.szrcwz.com                                                                                                                                           

                  

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-02-08
  • 2021-04-27
  • 2022-01-05
  • 2022-12-23
  • 2022-01-07
  • 2022-01-07
猜你喜欢
  • 2021-08-03
  • 2021-10-21
  • 2021-07-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
相关资源
相似解决方案