Java query access Database

Source: Internet
Author: User
Welcome to the Java Community Forum and interact with 2 million technical staff to enter packagechapter10; importjava. SQL. *; publicclassAccessData {publicAccessData () {} publicstaticvoidmain (String [] args) {newAccessData (); Stringurljdbc: odbc: mydbaccess; Stri

Welcome to the Java Community Forum and interact with 2 million technical staff> go to package chapter10; import java. SQL. *; public class AccessData {public AccessData () {} public static void main (String [] args) {new AccessData (); String url = "jdbc: odbc: mydbaccess"; Stri

Welcome to the Java Community Forum and interact with 2 million technical staff> enter

Package chapter10;

Import java. SQL .*;

Public class AccessData {

Public AccessData ()

{

}

Public static void main (String [] args ){

New AccessData ();

String url = "jdbc: odbc: mydbaccess ";

String query, name, mark;

Connection conn = null;

Statement statement = null;

ResultSet resultSet = null;

Try {

Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");

Conn = DriverManager. getConnection (url );

Statement = conn. createStatement ();

Query = "select * from student ";

ResultSet=statement.exe cuteQuery (query );

While (resultSet. next ())

{

Name = resultSet. getString ("name ");

Mark = resultSet. getString ("mark ");

System. out. print ("name:" + name );

System. out. println ("score:" + mark );

}

}

Catch (SQLException e)

{

E. printStackTrace ();

}

Catch (Exception e)

{

E. printStackTrace ();

}

Finally {

Try {

If (resultSet! = Null) resultSet. close ();

If (statement! = Null) statement. close ();

If (conn! = Null) conn. close ();

}

Catch (SQLException e)

{

E. printStackTrace ();

}

}

}

}

Display result:

Name: Li Ming score: 81

Name: Zhang San score: 85

Name: Li Si score: 90

Name: Wang Wu score: 75

Name: Chen Six score: 68

Name: Zhao Ba score: 90

Name: yellow 9 score: 10

Name: Sun Hong score: 82

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.