Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Cobb1;
- import java.sql.*;
- public class Cobb2 {
- private Connection cn;
- private Statement st;
- private ResultSet rs;
- public Cobb2(String path, String nameDB, String login, String pass) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- try {
- cn = DriverManager.getConnection(path + nameDB, login, pass);
- try {
- st = cn.createStatement();
- } catch (SQLException ex) {
- System.out.println("Error in Stayement" + ex);
- }
- } catch (SQLException ex) {
- System.out.println("Error in create Connection" + ex);
- }
- } catch (ClassNotFoundException ex) {
- System.out.println("Error in download Driver" + ex);
- }
- }
- public void update(String sql) {
- try {
- st.executeUpdate(sql);
- } catch (SQLException ex) {
- System.out.println("Error in Update" + ex);
- }
- }
- public ResultSet query(String sql) {
- try {
- rs = st.executeQuery(sql);
- } catch (SQLException ex) {
- System.out.println("Error in query" + ex);
- }
- return rs;
- }
- public void close() {
- try {
- st.close();
- cn.close();
- } catch (SQLException ex) {
- System.out.println("Error in close" + ex);
- }
- }
- }
- package Cobb1;
- import java.sql.*;
- public class Cobb1 {
- public static void main(String[] args) {
- Cobb2 cobb2 = new Cobb2("jdbc:mysql://127.0.0.1/", "my_db1", "root", "");
- ResultSet rs = cobb2.query("SELECT*FROM user");
- try {
- while (rs.next()) {
- System.out.println("id= " + rs.getInt(1) + "tname= " + rs.getString(2));
- }
- } catch (SQLException ex) {
- System.out.println("Error in rs" + ex);
- }
- cobb2.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement