JDBC : koneksi java ke database (basic)

Banyak temen-temen gua nanya tentang koneksi java ke database.. sampe bosen gua dengernya.. hehehe. neh gua kasih code sederhananya..

import java.sql.*;
public class ConSQLServer {
private Connection con;
private ResultSet rs;
private Statement stmt;
private int totalRec;
private String url="";
private String query="";
public ConSQLServer() {
if(url=="")
url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=EventManagement";
if(query=="")
query = "SELECT * FROM MyCompanyInformation";

try{
  con = DriverManager.getConnection(url,"sa","");
  stmt = con.createStatement();
  rs = stmt.executeQuery(query);
} catch(SQLException se){
  System.err.print("ClassNotFoundException:");
  System.err.println(se.getMessage());
}
}
...

Diatas code untuk menghubungan SQL Server dengan database/schema EventManagement. Perintah SQL-nya untuk mendapatkan record (SELECT) dari tabel MyCompanyInformation.. semoga semakin jelas..😀


One comment


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s