Skip to content

Instantly share code, notes, and snippets.

@sadv1r
Created August 7, 2014 11:05
Show Gist options
  • Save sadv1r/0b7a395eb94da0c9df74 to your computer and use it in GitHub Desktop.
Save sadv1r/0b7a395eb94da0c9df74 to your computer and use it in GitHub Desktop.
import java.sql.*;
import java.util.*;
public class Mysql {
private static final String CHARSET = "UTF-8";
private static String serverName = "localhost";
private static String portNumber = "3306";
private static String userName = "root";
private static String password = "";
private static String database = "VkSpy";
public void setServerName(String serverName) {
Mysql.serverName = serverName;
}
public void setPortNumber(int portNumber) {
Mysql.portNumber = String.valueOf(portNumber);
}
public void setUserName(String userName) {
Mysql.userName = userName;
}
public void setPassword(String password) {
Mysql.password = password;
}
public void setDatabase(String database) {
Mysql.database = database;
}
public Connection conn = getConnection();
private Connection getConnection() {
Connection conn = null;
Properties connectionProps = new Properties();
connectionProps.put("user", userName);
connectionProps.put("password", password);
try {
conn = DriverManager.getConnection("jdbc:mysql://" + serverName + ":" + portNumber + "/" + database + "?characterEncoding=" + CHARSET, connectionProps);
} catch (SQLException e) {
e.printStackTrace();
}
//System.out.println(conn);
return conn;
}
public ArrayList<int[]> getUsersId() {
Statement statement = null;
ResultSet resultSet = null;
ArrayList<int[]> result = new ArrayList<int[]>();
try {
statement = conn.createStatement();
resultSet = statement.executeQuery("select * from user");
while (resultSet.next()) {
result.add(new int[]{resultSet.getInt(1), resultSet.getInt(2)});
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
resultSet.close();
statement.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
public User getUserInfo(int id) {
User user = new User();
/*
* Ходим по таблицам полей и забиваем последний вариант поля в объект
*/
return user;
}
public void sentUserInfo(int id, String table, String value) {
try {
PreparedStatement pst = conn.prepareStatement("insert into " + table + " (id, value) values (?, ?)");
pst.setInt(1, id);
pst.setString(2, value);
pst.executeUpdate();
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void sentUserInfo(int id, String table, int value) {
try {
PreparedStatement pst = conn.prepareStatement("insert into " + table + " (id, value) values (?, ?)");
pst.setInt(1, id);
pst.setInt(2, value);
pst.executeUpdate();
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment