文档简介:
客户端连接
-
mysql客户端连接
mysql客户端连接
与mysql数据库的命令行操作方式一样,IP、端口、用户替换成dbproxy的IP、端口、用户
C:\Users\liuxh>mysql -h192.168.99.101 -P8866 -utest -ptest123!@# -c
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 24
Server version: 5.6.0-UDAL-DBPROXY-1.3.1-RELEASE-20170206-1131 DBProxy Server
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+----------+
| DATABASE |
+----------+
| TEST |
+----------+
1 row in set (0.00 sec)
mysql> use test;
Database changed
mysql> select * from employee;
Empty set (0.28 sec)
-
jdbc方式连接
jdbc方式连接
import java.sql.*; import java.util.HashMap; import java.util.Iterator;
public class DBProxyTest { public static void main(String[] args) throws Exception { Connection conn=null; Statement stmt = null; try{ conn= DriverManager.getConnection("jdbc:mysql://192.168.99.101:8866/mychema?useUnicode=true&characterEncoding=UTF8", "test", "test123!@#"); } catch (Exception e) { System.out.println("连接数据库失败, " + e.getMessage()); e.printStackTrace(); return; } //conn.setAutoCommit(false); 暂时不作事务的处理,事务的处理在使用UDAL中会详细描述 int result = 0; String sql = ""; stmt = conn.createStatement(); System.out.println("------ insert test begin -------"); stmt.executeUpdate("delete from employee"); for(int i = 0; i < 3; i ++){ sql = "insert into employee(employee_id,name,create_date) values(SEQ_TEST.nextval, 'test', now())"; result = stmt.executeUpdate(sql); System.out.println("insert result: " + result); } HashMap<String,String> map = DBProxyTest.printTableData(stmt); System.out.println("------ insert test end -------\n\n");
System.out.println("------ update test begin -------"); String updateSQL =""; String employee_id = ""; Iterator it=map.keySet().iterator(); while(it.hasNext()){ employee_id = it.next().toString(); updateSQL = "update employee set name = '" + map.get(employee_id) + employee_id + "' where employee_id = " + employee_id; result = stmt.executeUpdate(updateSQL); System.out.println("update result: " + result); } DBProxyTest.printTableData(stmt); System.out.println("------ update test end -------"); stmt.close(); conn.close(); }
public static HashMap<String,String> printTableData(Statement stmt) throws SQLException { HashMap map = new HashMap(); String sql = "SELECT * FROM employee order by employee_id"; ResultSet rs = stmt.executeQuery(sql); while(rs.next()){ String id = rs.getString("employee_id"); String name = rs.getString("name"); map.put(id,name); System.out.println("employee_id: " + id + " name: " + name); } return map; } } |