Simple connection Database Standard step __ Database

Source: Internet
Author: User

1: The method is not encapsulated in the case:

public class connectiondemo{

public static void Main (string[] args) {

1: Set parameters

String url= "Jdbc:mysql://localhost:3306/dbname?useunicode=true&characterencoding=utf8";

String user= "root";

String pwd= "123456";

Connection Conn=null;

PreparedStatement Pstmt=null;

ResultSet Rs=null;

try{

2 Load Driver

Class.forName ("Com.mysql.jdbc.Driver");

3 Getting Connection objects

Conn=drivermanager.getconnection (URL,USER,PWD);

4 Sending SQL statements

String sql= "SELECT * from user where id=?";

Pstmt=conn.preparestatement (SQL);

5 Setting parameters

Pstmt.setnameid (1);

Pstmt.setname ("Lisi");

6 execution

Rs.pstmt.executeQuery ();

7 Extraction Results

while (Rs.next ()) {

int Use_id=rs.getint (); First column

String use_name=rs.getstring ("Usr_name");

System.out.println (use_id+ "T" +use_name);

}

}catch (ClassNotFoundException e) {

E.printstacktrace ();

System.out.println ("Load driven failure");

}catch (SQLException e) {

E.printstacktrace ();

SYSTEM.OUT.PRINTLN ("Get Connection object failed.") ");

}

}

}

}

Related Article

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.