Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,155 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,807 people online right now. Registration is fast and FREE... Join Now!




Connecting database

 
Reply to this topicStart new topic

Connecting database, login in to database

lemar
post 10 Oct, 2008 - 05:49 AM
Post #1


New D.I.C Head

*
Joined: 8 Nov, 2007
Posts: 1


My Contributions


CODE

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

public class Main {
  public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement();
    // st.executeUpdate("drop table survey;");
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");

    st = conn.createStatement();
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    ResultSetMetaData rsMetaData = rs.getMetaData();

    int numberOfColumns = rsMetaData.getColumnCount();
    System.out.println("resultSet MetaData column Count=" + numberOfColumns);

    st.close();
    conn.close();
  }

  private static Connection getConnection() throws Exception {
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    String url = "jdbc:odbc:northwind";
    String username = "";
    String password = "";
    Class.forName(driver);
    return DriverManager.getConnection(url, username, password);
  }
}

User is offlineProfile CardPM

Go to the top of the page

abgorn
post 10 Oct, 2008 - 06:20 AM
Post #2


Hello Crap for Brains

Group Icon
Joined: 5 Jun, 2008
Posts: 858



Thanked 4 times

Dream Kudos: 25
My Contributions


What's the question
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/21/08 01:53PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month