MySQL Connector ile Java - MySQL bağlantısı
Submitted by yasin on Mon, 03/23/2009 - 10:13
IntelliJ IDEA adlı IDE hayatı gerçekten kolaylaştırıyor. Önce Settings > Project Settings > Libraries'den bir Library ekliyoruz. Adı mühim değil, "atach" yaptığımız class mysqlconnector. Bunu da şuradan indirebiliyoruz.
Ardından OK dedikten sonra, gerekli kodlar şöyle;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Main {
public static void main(String[] args){
Connection con = null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://host_adi:3306/veritabani_adi","kullanici_adi","sifre");
if(!con.isClosed())
System.out.println("Successfully connected to MySQL server using TCP/IP...");
} catch (Exception e) {
e.printStackTrace();
//System.err.println("Exception: "+e.getMessage());
} finally {
try{
if(con != null){
con.close();
}
} catch(SQLException se) {
}
}
}
}
