Basic Database Connection Java

Source: Internet
Author: User

 

 

Import Java. SQL. *; </P> <p> public class dbtest2 <br/> {<br/> string url = "JDBC: mysql: // localhost: 3306 /"; <br/> string user = "root"; <br/> string passwd = "root"; <br/> Public dbtest2 () throws classnotfoundexception <br/>{< br/> class. forname ("com. mySQL. JDBC. driver "); <br/>}< br/> Public static void main (string [] ARGs) <br/>{< br/>}< br/>} 

 

 

<Br/> Import Java. io. *; <br/> Import Java. SQL. *; <br/> public class connectiondemo <br/>{< br/> Public static void main (string [] ARGs) <br/>{< br/> string sql1 = "select * from A1"; <br/> string sql2 = "insert into A1 (AA, BB) values (null, 'aaa') "; <br/> try {<br/> dbsource = new simpledbsource (); <br/> connection conn = dbsource. getconnection (); <br/> statement stmt = Conn. createstatement (); <br/> resu Ltset rs = stmt.exe cutequery (sql1); <br/> resultsetmetadata rmeta = Rs. getmetadata (); <br/> int numcolumns = rmeta. getcolumncount (); <br/> system. out. println (numcolumns); <br/> // Rs. next (); <br/> // system. out. println (RS. getstring (1); <br/> // system. out. println (); <br/> while (RS. next () <br/>{< br/> // Rs. next (); <br/> // string str1 = Rs. getstring (0); <br/> // int a1 = Rs. getint (0); <br/> // system. out. println (Str1); <br/> // system. out. println (A1); <br/> for (INT I = 0; I <numcolumns; I ++) <br/>{< br/> string stemp = Rs. getstring (I + 1); <br/> system. out. println (stemp + ""); <br/>}< br/> system. out. println (); </P> <p >}< br/> for (INT I = 0; I <10000; I ++) <br/>{< br/> stmt.exe cuteupdate (sql2); <br/>}< br/> If (! Conn. isclosed () <br/>{< br/> system. out. println ("database connection enabled"); <br/>}< br/> dbsource. closeconnection (conn); <br/> If (Conn. isclosed () <br/>{< br/> system. out. println ("Database Connection closed"); <br/>}< br/>/*} catch (ioexception e) {<br/> E. printstacktrace (); */<br/>} catch (classnotfoundexception e) {<br/> E. printstacktrace (); <br/>} catch (sqlexception e) {<br/> E. printstacktrace (); <br/>}</P> <p> Import Java. SQL. *; </P> <p> Public interface dbsource {<br/> Public connection <br/> getconnection () throws sqlexception; <br/> Public void <br/> closeconnection (connection conn) throws sqlexception; <br/>}</P> <p> Import Java. util. *; <br/> Import Java. SQL. *; <br/> Import Java. io. *; </P> <p> public class simpledbsource implements dbsource <br/>{< br/> string URL; <br/> string user; <br/> string passwd; <br/> Public simpledbsource () throws classnotfoundexception <br/> {<br/> url = "JDBC: mysql: // localhost: 3306/demo "; <br/> User = "root"; <br/> passwd = "root"; <br/> class. forname ("com. mySQL. JDBC. driver "); <br/>}< br/> Public connection <br/> getconnection () throws sqlexception <br/>{< br/> return drivermanager. getconnection (URL, user, passwd); <br/>}< br/> Public void <br/> closeconnection (connection conn) throws sqlexception <br/>{< br/> Conn. close (); <br/>}< br/>} 

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.