/** * */package com.xx.db;/** * @author
* Implement database Connection * Implement data query and update (increase and deletion) * Use only one database to write dead in this file; If you use multiple databases, you can use a configuration file to implement */import Java.sql.connection;import Java.sql.drivermanager;import Java.sql.resultset;import Java.sql.sqlexception;import Java.sql.Statement;import Java.util.arraylist;import Java.util.list;//import Org.apache.commons.lang.stringutils;//import Org.testng.annotations.test;public class Dbutils {//driver name public String dbdriver = "com.mysql.jdbc.Driver";// The URL points to the database name that you want to access public String URL = "Jdbc:mysql://192.168.xx.xx:3306/database";p ublic string user = "username";p ublic String password = "password"; /* * MySQL config username username user * Java connection mysql configuration password password */public Connection connect () {try {//Load driver Class.forName (dbdriver); /Connection Database Connection conn = drivermanager.getconnection (URL, user, password);//if (!conn.isclosed ()) {//print every time, too annoying , so commented out the//system.out.println ("succeeded connecting to the database!"); /}return Conn;} catch (ClassNotFoundException e) {System.err.println ("Sorry,can ' t find the driver!"); E.printstacktrace (); return null;} catch (SQLException e) {e.printstacktrace (); return null;} catch (Exception e) {e.printstacktrace (); return null;}} /* * Get query results ResultSet */public ResultSet selectresult (String sql) throws sqlexception{//statement used to execute SQL statements statement statement = Connect (). createstatement ();//SQL statement to execute resultset rs = statement.executequery (SQL); return RS;} /* * Get query Results by column */public list<string> getstringlist (String sql, String key) throws Sqlexception{list<string> VA Lue = new arraylist<string> (); ResultSet rs = selectresult (sql), while (Rs.next ()) {//Select key for this column of data Value.add (rs.getstring (key));} Rs.close (); Connect (). Close (); return value;} Gets the number of bars for SQL execution Results public int getcount (String sql, String PrimaryKey) throws Sqlexception{int count = 0;count = Getstringlist (Sql,primarykey). Size (); return count;} Update database for INSERT, UPDATE, Deletepublic boolean updateresult (String sql) throws sqlexception{// Statement is used to execute SQL statements statement statement = Connect (). creatEstatement (); int rs = 0;//The SQL statement to execute, to prevent full table updates, to determine if there is a WHERE clause in the SQL statement, the INSERT statement does not have to have a WHERE clause if (Sql.contains ("Update") | | Sql.contains ("delete")) {if (Sql.contains ("WHERE") | | Sql.contains ("where")) {rs = statement.executeupdate (sql);}} else if (Sql.contains ("INSERT INTO")) {rs = statement.executeupdate (sql);} Statement.close (); Connect (). Close (); if (rs==1) {return true;} else return false;} @Test//public void Test () throws Sqlexception{//string sql = "SELECT * from TableName WHERE c_username= ' user '";//list< ; string> value =getstringlist (SQL, "username");//system.out.println (value.tostring ());//system.out.println ( GetCount (SQL, "C_uid"));////string sql2 = "Update shop_tablename set a= ' 1101 ' WHERE username= ' user '";//string sql3 = "Inse RT into TableName (id,name) VALUES (9999999, ' one ') ";//string sql4 =" Delete from TableName WHERE name= ' one ' ";////system.out. println (Updateresult (SQL3));////system.out.println (Sql4.contains ("WHERE"));/}}
Welcome to the http://shop105984718.taobao.com/of Jiao Jiao Jia's clothing court
Java Operations Database