Simple database connection

Source: Internet
Author: User

Package demo;

/**

* <P> title: </P>

* <P> Description: </P>

* <P> copyright: Copyright (c) 2005 </P>

* <P> company: </P>

* @ Author not attributable

* @ Version 1.0

*/

Import java. SQL .*;

Public class printdata {

Public printdata (){

}

Public static void main (string [] ARGs ){

Connection con;

Statement SQL;

Resultset RS;

Try {

Try {

Class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver ");

}

Catch (classnotfoundexception ex1 ){

Ex1.printstacktrace ();

}

Con = drivermanager. getconnection ("JDBC: ODBC: Test", "sa ","");

SQL = con. createstatement ();

Rs = SQL .exe cutequery ("select top 10 * from MERs ");

While (Rs. Next ()){

System. Out. Print (Rs. getstring ("customerid "));

System. Out. println ();

}

Con. Close ();

}

Catch (sqlexception ex ){

Ex. printstacktrace ();

}

}

}

Note:

If class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver") is not added ");

An error is prompted.

Java. SQL. sqlexception: no suitable driver

At java. SQL. drivermanager. getconnection (drivermanager. Java: 532)

At java. SQL. drivermanager. getconnection (drivermanager. Java: 171)

At demo. printdata. Main (printdata. Java: 21)

Class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver"); // Add this sentence

String url = "JDBC: ODBC: Student ";

Connection conn = drivermanager. getconnection (URL ,"","");

Register the driver before connection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.