본문 바로가기
백엔드기술/개발언어

Connect sample

by RevFactory 2009. 12. 9.

package java_samples_hwang;
import  java.sql.*;
/**
 *
 * @author hwang
 */
public class Connect {
    public static void main(String arg[])
    {
        Connection con = null;
        Statement statement;
        try
        {
            String userName = "root";
            String password = "root123";
            String url = "jdbc:mysql://localhost:3306/Master";
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con = DriverManager.getConnection(url, userName, password);
             System.out.println("Connection with MySql established");
              statement = con.createStatement();
              boolean foundResults = statement.execute("SELECT * FROM emp");
        if(foundResults)
  {
           ResultSet set = statement.getResultSet();
           if(set!=null)
                 {
                     while(set.next())
                     {
                         String sId = set.getString("empid");
                         String sName = set.getString("empname");
                         String sAddress = set.getString("empadd");
                         System.out.println("ID = " + sId + " NAME = " + sName + " ADDRESS = " +sAddress);
                     }
                 }
                 }
        }
       
        catch(Exception e)
        {
            System.out.println("Could not connect my MySql");
            System.out.println("Error msg:"+e.toString());
            e.printStackTrace();
        }
        finally
        {
            if(con!=null)
            {
                try
                {
                    con.close();
                }
                catch(Exception e)
                {
                }
        }
        }
    }

}

'백엔드기술 > 개발언어' 카테고리의 다른 글

InheritanceDemo  (0) 2009.12.09
ArrayDemo  (0) 2009.12.09
Modularization  (0) 2009.12.09