Java JDBC Operation MySQL

Source: Internet
Author: User


Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;

public class Test {
public static final String dbdriver = "Com.mysql.jdbc.Driver";
Define the connection address of the MySQL database
public static final String Dburl = "Jdbc:mysql://localhost:3306/test"; Connect to a database named T in native MySQL
Connection user name for MySQL database
public static final String DBUSER = "root";
Connection password for MySQL database
public static final String Dbpass = "123456";

public static void Main (String args[]) {

PreparedStatement PS = null;
ResultSet rs = null;
Connection ct = null;

try {
Load Driver
Class.forName (Dbdriver);
Get Connected
ct = drivermanager.getconnection (dburl,dbuser,dbpass);
String sql = "SELECT * from User_login where id=?";
Ps=ct.preparestatement (SQL);
Ps.setstring (1, "1");

Rs=ps.executequery ();
while (Rs.next ()) {
String name = rs.getstring ("username");
SYSTEM.OUT.PRINTLN (name);
}

} catch (Exception e) {
E.printstacktrace ();
}
}
}

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.