Java access database connection example

Source: Internet
Author: User
Importjava. SQL. *; publicclassCoalTotals {*** @ paramargs * publicstaticvoidmain (String [] args) {TODOAuto-generatedmethodstubtry {load the driver Class supported by the database. forName (sun. jdbc. odbc. jdbcOdbcDriver); Stringurljdbc: odbc: Drive

Import java. SQL. *; public class CoalTotals {/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stubtry {// load the driver Class supported by the database. forName ("sun. jdbc. odbc. jdbcOdbcDriver "); String url =" jdbc: odbc: Drive

Import java. SQL. *; public class CoalTotals {/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stubtry {// load the driver Class supported by the database. forName ("sun. jdbc. odbc. jdbcOdbcDriver "); String url =" jdbc: odbc: Driver = {Microsoft Access Driver (*. mdb)}; DBQ = E: // Dev // WorkSpace // chapter21 // bin // worlddeskmdb "; try {// establish a data source Connection conn = DriverManager. getConnection (url, "", ""); Statement st = conn. createStatement (); ResultSet rec = st.exe cuteQuery ("select *" + "from Coal"); System. out. println ("FIPS/tCOUNTRY/tYEAR/t" + "anthracite production"); while (rec. next () {System. out. println (rec. getString (1) + "/t" + rec. getString (2) + "/t" + rec. getString (3) + "/t" + rec. getString (4);} st. close ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}

The url Connection here must be careful not to use multiple spaces with only one space. Because when I write an extra space, the system will prompt that the file name is incorrect.

Other steps to connect to the database are basically no big problem ~~

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.