Database Lookup Operation-java

Source: Internet
Author: User
Tags stmt

import java.sql.Connection;Import Java.sql.drivermanager;import java.sql.resultset;import java.sql.sqlexception;import java.sql.Statement; public class Sqldemo {public static void Main (string[] args) {try {//1, load driver//Driver name: Com.mysql.jdbc.DriverClass.forName ("Com.mysql.jdbc.Driver");//2, establishment and database connection//sql user name: Root//sql user password: 8273322Connection con = drivermanager.getconnection ("Jdbc:mysql://127.0.0.1:3306/store?characterencoding=utf8&usessl=false", "root", "8273322");//(1) Find operation:String sql = "SELECT * from Good_list";/* Statement is an important interface for Java to perform database operations.used to send the SQL statement to be executed to the database, based on the database connection already established. Statement object for executing simple SQL statements with no arguments*/Statement stmt = Con.createstatement ();/*ResultSet, a data table for a database result set, typically generated by executing statements that query the database. */ResultSet rs = stmt.executequery (SQL);//Traversal outputwhile (Rs.next ()) {//or: Int id = rs.getint ("id"); ID is data segment nameint id = rs.getint (1);String name = rs.getstring (2);String pw = rs.getstring (3);System.out.println (id+ "" +name+ "" +PW ");}//OffRs.close ();Stmt.close ();Con.close ();} catch (ClassNotFoundException e) {E.printstacktrace ();} catch (SQLException e) {E.printstacktrace ();}}}

Database Lookup Operation-java

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.