Java connects MySQL database with JDBC

Source: Internet
Author: User

First Class:

Package dina.web.test;

Import java.sql.*;

public class DBHelper {
public static final String DRIVER = "Com.mysql.jdbc.Driver";
public static final String URL = "Jdbc:mysql://58.215.50.62:3307/iov";
public static final String USER = "Iov_user";
public static final String PASSWORD = "2wsx3edc";

public Connection conn = null;
public PreparedStatement PST = NULL;

Public dbhelper (String sql) {
try {
Class.forName (DRIVER);
conn = Drivermanager.getconnection (URL, USER, PASSWORD);
PST = conn.preparestatement (SQL);
} catch (Exception e) {
E.printstacktrace ();
}
}

public void Close () {
try {
if (PST! = null) {
This.pst.close ();
}

} catch (Exception e) {
E.printstacktrace ();
}
try {
IF (conn! = null) {
This.conn.close ();
}
} catch (Exception e) {
E.printstacktrace ();
}

}
}

A second class:

Package dina.web.test;

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;


public class Sqlexcute {

static DBHelper db = null;
static String sql = NULL;
static ResultSet rs = null;

public static void Main (string[] args) {
sql = "SELECT * from Sys_user where username= ' 15020151106 '";
db = new dbhelper (SQL);
try {
Rs=db.pst.executequery ();
while (Rs.next ()) {

SYSTEM.OUT.PRINTLN ("Account Number:" +rs.getstring (3));
System.out.println ("Name:" +rs.getstring (4));
}
Rs.close ();
Db.close ();

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

}

Java connects MySQL database with JDBC

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.