mycat 做mysql中间件
命令行 可以连接上,
程序无法连接
命令行如下图
程序代码:
命令行 可以连接上,
程序无法连接
命令行如下图
程序代码:
package com.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DbConnectionTest { private static String url = "jdbc:mysql://192.168.1.212:8066/TESTDB?useUnicode=true&characterEncoding=utf-8"; private static String username = "test"; private static String password = "test"; private static String driverName = "com.mysql.jdbc.Driver"; public DbConnectionTest() { super(); // TODO Auto-generated constructor stub } static { try { Class.forName(driverName); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url, username, password); } public static void close(ResultSet rs, Statement st, Connection conn) { try { if (rs != null) { rs.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (st != null) { st.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } public static void main(String[] args) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = getConnection(); String sql = "select * from testaaaaa"; ps = conn.prepareStatement(sql); // ps.setString(1, "psName"); // ps.setString(2, "psPassword"); // ps.setString(3, "jkjs@126.com"); // ps.setInt(4, 23); // ps.setDate(5, new java.sql.Date(new java.util.Date().getDate())); // ps.setFloat(6, 2344); rs = ps.executeQuery(sql); while(rs.next()){ System.out.println(rs.getString("name")); } } catch (SQLException e) { e.printStackTrace(); } finally { close(rs, ps, conn); } } }