JDBC Operation MySQL database instance _jsp programming

Source: Internet
Author: User

The example in this article describes the JDBC operation method for MySQL database. Share to everyone for your reference. as follows:

Import java.sql.*;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
 Class conn{String url= "Jdbc:mysql://127.0.0.1/naridata";
 Final String user = "Nari";
 Final String pass = "NARIACC";
 Connection conn = null;
 Statement st = null;
 ResultSet rs = null;
  Conn () {try{class.forname ("Com.mysql.jdbc.Driver");
  }catch (Exception e) {e.printstacktrace ();
   } try{System.out.println ("in Dbmanager");
   conn = drivermanager.getconnection (URL, user, pass);
   String sql = "CREATE TABLE test (ID int, uid int)";
   st = Conn.createstatement ();
  St.execute (SQL);
  }catch (Exception e) {e.printstacktrace ();
    } void Insert () {try{for (int i=0;i<10;i++) {String sql = ' INSERT into test values (' +i+ ', ' +i+ ') ';
   St.execute (SQL);
  }}catch (Exception e) {e.printstacktrace ();
   } void Query () {try{String sql = ' select * from Test ';
   rs = st.executequery (SQL);
  while (Rs.next ()) {  System.out.print ("Res1:" +rs.getint (1) + "Res2:" +rs.getint (2) + "\ n");
  }}catch (Exception e) {e.printstacktrace ();
   } void Close () {try{rs.close ();
   St.close ();
  Conn.close ();
  }catch (Exception e) {e.printstacktrace ();  }} public class Mysqltest {public static void main (string[] args) {//TODO auto-generated method Stub conn =
  New Conn ();
  A1.insert ();
  A1.query ();
 A1.close ();

 }
}

I hope this article will help you with your JSP programming.

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.