Java uses MySQL database code

Source: Internet
Author: User


Import java.sql.*;
Import java.util.ArrayList;
Import java.util.List;

Import Com.mysql.jdbc.ResultSetMetaData;
public class Mysqlhelper {


Test connection function
public static void Main (String []args)
{
Derbyhelper de=new derbyhelper ();
String sql= "SELECT * from users";

List<object[]> list=de.querylist (SQL, NULL);
for (int i=0;i<list.size (); i++)
{Object []obj=list.get (i);
System.out.println (obj[1].tostring () + "" +obj[2].tostring ());
}

}

MySQL Connection property settings
String driver= "Com.mysql.jdbc.Driver";
String url= "Jdbc:mysql://192.168.1.114:3306/test";
String user= "root";
String passwd= "1234";
Connection classes and Interfaces
Connection CT;
PreparedStatement PS;
ResultSet rs;

Public Mysqlhelper ()
{
try {
Class.forName (driver);
Ct=drivermanager.getconnection (URL,USER,PASSWD);
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
public int Update (String sql,string []paras)
{
int count=0;
try {
Ps=ct.preparestatement (SQL);
Injection parameters
This.setparameters (paras);
Gets the number of updated data rows.
Count=ps.executeupdate ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
This.close ();
}
return count;
}


/**
* @param sql
* @param paras No injection parameters can be null or empty string.
* @return Returns a resultset result set.
*/
Public ResultSet query (String sql,string[] paras)
{
try {
Ps=ct.preparestatement (SQL);
This.setparameters (paras);
Rs=ps.executequery ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
Not off.
}


Return RS;
}
/**
* @param sql
* @param paras No injection parameters can be null or empty string.
* @return Returns a list containing an array of objects.
*/
Public List querylist (String sql,string []paras)
{
List<object[]> list=new arraylist<object[]> ();
try {
Ps=ct.preparestatement (SQL);
This.setparameters (paras);
Rs=ps.executequery ();
ResultSetMetaData rsmd= (ResultSetMetaData) rs.getmetadata ();
int Count=rsmd.getcolumncount ();
Place the query results in an array of objects.
while (Rs.next ())
{int rows= rs.getrow ();

Object Obj[]=new Object[count];
for (int j=0;j<count;j++)
{obj[j]=rs.getstring (j+1);

}
List.add (obj);
}
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{this.close ();}

return list;}

public void Close ()
{

try {
if (rs!=null) rs.close ();
if (ps!=null) ps.close ();
if (ct!=null) ct.close ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}

private void Setparameters (String[] paras) throws SQLException {
if (Paras!=null &&!paras.equals (""))
{for (int i=0;i<paras.length;i++)
{
Ps.setstring (i+1, paras[i]);
}
}
}}

Java uses MySQL database code

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.