Ok - I found the driver version that goes with the database.. however now I get the following.
好的 - 我找到了与数据库一起使用的驱动程序版本..但是现在我得到以下内容。
Got an exception! Communications link failure due to underlying exception:
有一个例外!基础异常引起的通信链路故障:
** BEGIN NESTED EXCEPTION **
**开始没有异常**
java.net.ConnectException MESSAGE: Connection timed out: connect
java.net.ConnectException消息:连接超时:连接
STACKTRACE:
java.net.ConnectException: Connection timed out: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) at java.net.Socket.connect(Socket.java:525) at java.net.Socket.connect(Socket.java:475) at java.net.Socket.(Socket.java:372) at java.net.Socket.(Socket.java:215) at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256) at com.mysql.jdbc.MysqlIO.(MysqlIO.java:271) at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771) at com.mysql.jdbc.Connection.(Connection.java:1555) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285) at java.sql.DriverManager.getConnection(DriverManager.java:582) at java.sql.DriverManager.getConnection(DriverManager.java:185) at freelancebillingapp.customerInfoUI.jButton1MouseClicked(customerInfoUI.java:221) at freelancebillingapp.customerInfoUI.access$000(customerInfoUI.java:12) at freelancebillingapp.customerInfoUI$1.mouseClicked(customerInfoUI.java:59) at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253) at java.awt.Component.processMouseEvent(Component.java:6266) at javax.swing.JComponent.processMouseEvent(JComponent.java:3255) at java.awt.Component.processEvent(Component.java:6028) at java.awt.Container.processEvent(Container.java:2041) at java.awt.Component.dispatchEventImpl(Component.java:4630) at java.awt.Container.dispatchEventImpl(Container.java:2099) at java.awt.Component.dispatchEvent(Component.java:4460) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4247) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168) at java.awt.Container.dispatchEventImpl(Container.java:2085) at java.awt.Window.dispatchEventImpl(Window.java:2475) at java.awt.Component.dispatchEvent(Component.java:4460) at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
java.net.ConnectException:连接超时:通过java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java)上的java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)连接java.net.PlainSocketImpl.socketConnect(Native Method)(PlainSocketImpl.java) :195)java.net.SocksSocketImpl.connect(PlainSocketImpl.java:182)java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)java.net.Socket.connect(Socket.java:525)java java.net.Socket。(Socket.java:372)的.net.Socket.connect(Socket.java:475)at java.net.Socket。(Socket.java:215)at com.mysql.jdbc.StandardSocketFactory。在com.mysql.jdbc.MysqlIO(MysqlIO.java:271)的com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)com.mysql.jdbc.Connection上连接(StandardSocketFactory.java:256)。 (Connection.java:1555)位于java.sql.DriverManager.getConnection(DriverManager。)的java.sql.DriverManager.getConnection(DriverManager.java:582)中的com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)。 java:185)at freelancebilling来自freelancebillingapp.customerInfUI上的app.customerInfoUI.jButton1MouseClicked(customerInfoUI.java:221)$ free(customerInfoUI.java:12)at freelancebillingapp.customerInfoUI $ 1.mouseClicked(customerInfoUI.java:59)at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster) .java:253)在javax.swing.JComponent.processMouseEvent(JComponent.java:3255)的java.awt.Component.processMouseEvent(Component.java:6266)java.awt.Component.processEvent(Component.java:6028)在java.awt.Container.processEvent(Container.java:2041)的java.awt.Component.dispatchEventImpl(Component.java:4630)at java.awt.Container.dispatchEventImpl(Container.java:2099)at java.awt。 java.awt.LightweightDispatcher上的Component.dispatchEvent(Component.java:4460)java.awt.LightweightDispatcher.processMouseEvent(Container.java:4574)java.awt.LightweightDispatcher.dispatchEvent上的java.awt.LightweightDispatcher.processMouseEvent(Container.java:4247) .java:4168)java.awt.Win上的java.awt.Container.dispatchEventImpl(Container.java:2085) dow.dispatchEventImpl(Window.java:2475)at java.awt.Component.dispatchEvent(Component.java:4460)at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread .java:269)java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
** END NESTED EXCEPTION **
**结束嵌套异常**
Last packet sent to the server was 1 ms ago.
发送到服务器的最后一个数据包是1毫秒前。
3 个解决方案
#1
3
This may not solve it, but it tells you that someone else has had this problem.
这可能无法解决,但它会告诉您其他人遇到此问题。
Make sure you have the precise version of JDBC driver to match your version of MySQL.
确保您具有与您的MySQL版本匹配的JDBC驱动程序的精确版本。
I would strongly urge you to rewrite your code more like this. You aren't closing resources properly at all.
我强烈建议你更改这样的代码。您根本没有正确关闭资源。
Adapt it to your own needs. I created a local MySQL database on my machine and added a customer table. It worked just fine.
根据您自己的需要进行调整。我在我的机器上创建了一个本地MySQL数据库并添加了一个客户表。它运作得很好。
package persistence;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class DatabaseUtils
{
private static final String URL = "jdbc:mysql://localhost:3306/contacts";
private static final String USERNAME = "contacts";
private static final String PASSWORD = "contacts";
public static final String SELECT_SQL = "select customer_id, name, street, city, state, zip, phone, url from customer order by customer_id";
public static final String INSERT_SQL = "insert into customer(name, street, city, state, zip, phone, url) values(?,?,?,?,?,?,?)";
public static void main(String[] args)
{
Connection connection = null;
try
{
connection = getConnection(URL, USERNAME, PASSWORD);
List<Map> rows = findAllCustomers(connection);
for (Map row : rows)
{
System.out.println(row);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
close(connection);
}
}
public static List<Map> findAllCustomers(Connection connection) throws SQLException
{
List<Map> rows = new ArrayList<Map>();
PreparedStatement st = null;
ResultSet rs = null;
try
{
st = connection.prepareStatement(SELECT_SQL);
rs = st.executeQuery();
while (rs.next())
{
rows.add(map(rs));
}
}
finally
{
close(rs);
close(st);
}
return rows;
}
private static Map<String, Object> map(ResultSet rs) throws SQLException
{
Map<String, Object> row = new LinkedHashMap<String, Object>();
ResultSetMetaData meta = rs.getMetaData();
int numColumns = meta.getColumnCount();
for (int i = 1; i <= numColumns; ++i)
{
String column = meta.getColumnName(i);
Object value = rs.getObject(i);
row.put(column, value);
}
return row;
}
public static Connection getConnection(String url, String username, String password) throws SQLException
{
Driver driver = DriverManager.getDriver(url);
DriverManager.registerDriver(driver);
return DriverManager.getConnection(url, username, password);
}
public static void close(Connection connection)
{
try
{
if (connection != null)
{
connection.close();
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public static void close(Statement st)
{
try
{
if (st != null)
{
st.close();
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public static void close(ResultSet rs)
{
try
{
if (rs != null)
{
rs.close();
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public void rollback(Connection connection)
{
try
{
if (connection != null)
{
connection.rollback();
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
#2
0
Can mysql accept network connections? When you connect with the "mysql" command line program, you're not doing a network connection, but when you do with JDBC you are making a network connection. Try using "-h localhost" in your mysql command line to see.
mysql可以接受网络连接吗?当您使用“mysql”命令行程序连接时,您没有进行网络连接,但是在使用JDBC时,您正在建立网络连接。尝试在mysql命令行中使用“-h localhost”查看。
#3
0
/*
Connection
*/
import com.mysql.jdbc.Connection;
import java.sql.DriverManager;
public class PersonalConnection {
private String url = "jdbc:mysql://localhost:3306/";
private String schema = "database name";
private String uname = "";
private String password = "";
private Connection connection;
public Connection openConnection() {
try {
try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
} catch (Exception ex) {
}
connection = (Connection) DriverManager.getConnection(url + schema, uname, password);
} catch (Exception ex) {
}
return connection;
}
public void closeConneciton() {
try {
connection.close();
} catch (Exception ex) {
}
}
}
/* Servlet */
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
DetailsBean db = new DetailsBean();
String name = request.getParameter("name");
String countryname = request.getParameter("countryname");
String statename = request.getParameter("statename");
db.setName(name);
db.setCountry(countryname);
db.setState(statename);
DetailsManager dm = new DetailsManager();
String result = dm.insertDetailsManager(db);
if (result.equals("true")) {
/* RequestDispatcher rd = request.getRequestDispatcher("StateDetails.jsp?name=" + db.getName());
rd.forward(request, response);*/
response.sendRedirect("StateDetails.jsp");
} else {
out.print(result);
}
/* DAO */
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import com.mysql.jdbc.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DetailsDAO {
private Connection connection;
private PersonalConnection con;
public DetailsDAO() {
con = new PersonalConnection();
}
public String insertRecordDAO(DetailsBean db) {
int rowcount=0;
try {
connection=con.openConnection();
String query = "INSERT INTO personal_details(user_name,country_name,state_name) values(?,?,?);";
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, db.getName());
ps.setString(2, db.getCountry());
ps.setString(3, db.getState());
rowcount = ps.executeUpdate();
if(rowcount==1)
{
return "true";
}
else
{
return "false";
}
} catch (Exception ex) {
return ex+"";
} finally {
con.closeConneciton();
}
}
public ResultSet getGetails() throws Exception
{
connection=con.openConnection();
String query="SELECT * from state_details";
PreparedStatement ps=connection.prepareStatement(query);
ResultSet rs=ps.executeQuery();
return rs;
}
}
/*Manager*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.ArrayList;
public class DetailsManager {
DetailsDAO detdao = new DetailsDAO();
public String insertDetailsManager(DetailsBean db) {
String rowcount = detdao.insertRecordDAO(db);
return rowcount;
}
}
/*Bean*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
public class DetailsBean {
private String name;
private String country;
private String state;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
#1
3
This may not solve it, but it tells you that someone else has had this problem.
这可能无法解决,但它会告诉您其他人遇到此问题。
Make sure you have the precise version of JDBC driver to match your version of MySQL.
确保您具有与您的MySQL版本匹配的JDBC驱动程序的精确版本。
I would strongly urge you to rewrite your code more like this. You aren't closing resources properly at all.
我强烈建议你更改这样的代码。您根本没有正确关闭资源。
Adapt it to your own needs. I created a local MySQL database on my machine and added a customer table. It worked just fine.
根据您自己的需要进行调整。我在我的机器上创建了一个本地MySQL数据库并添加了一个客户表。它运作得很好。
package persistence;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class DatabaseUtils
{
private static final String URL = "jdbc:mysql://localhost:3306/contacts";
private static final String USERNAME = "contacts";
private static final String PASSWORD = "contacts";
public static final String SELECT_SQL = "select customer_id, name, street, city, state, zip, phone, url from customer order by customer_id";
public static final String INSERT_SQL = "insert into customer(name, street, city, state, zip, phone, url) values(?,?,?,?,?,?,?)";
public static void main(String[] args)
{
Connection connection = null;
try
{
connection = getConnection(URL, USERNAME, PASSWORD);
List<Map> rows = findAllCustomers(connection);
for (Map row : rows)
{
System.out.println(row);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
close(connection);
}
}
public static List<Map> findAllCustomers(Connection connection) throws SQLException
{
List<Map> rows = new ArrayList<Map>();
PreparedStatement st = null;
ResultSet rs = null;
try
{
st = connection.prepareStatement(SELECT_SQL);
rs = st.executeQuery();
while (rs.next())
{
rows.add(map(rs));
}
}
finally
{
close(rs);
close(st);
}
return rows;
}
private static Map<String, Object> map(ResultSet rs) throws SQLException
{
Map<String, Object> row = new LinkedHashMap<String, Object>();
ResultSetMetaData meta = rs.getMetaData();
int numColumns = meta.getColumnCount();
for (int i = 1; i <= numColumns; ++i)
{
String column = meta.getColumnName(i);
Object value = rs.getObject(i);
row.put(column, value);
}
return row;
}
public static Connection getConnection(String url, String username, String password) throws SQLException
{
Driver driver = DriverManager.getDriver(url);
DriverManager.registerDriver(driver);
return DriverManager.getConnection(url, username, password);
}
public static void close(Connection connection)
{
try
{
if (connection != null)
{
connection.close();
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public static void close(Statement st)
{
try
{
if (st != null)
{
st.close();
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public static void close(ResultSet rs)
{
try
{
if (rs != null)
{
rs.close();
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public void rollback(Connection connection)
{
try
{
if (connection != null)
{
connection.rollback();
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
#2
0
Can mysql accept network connections? When you connect with the "mysql" command line program, you're not doing a network connection, but when you do with JDBC you are making a network connection. Try using "-h localhost" in your mysql command line to see.
mysql可以接受网络连接吗?当您使用“mysql”命令行程序连接时,您没有进行网络连接,但是在使用JDBC时,您正在建立网络连接。尝试在mysql命令行中使用“-h localhost”查看。
#3
0
/*
Connection
*/
import com.mysql.jdbc.Connection;
import java.sql.DriverManager;
public class PersonalConnection {
private String url = "jdbc:mysql://localhost:3306/";
private String schema = "database name";
private String uname = "";
private String password = "";
private Connection connection;
public Connection openConnection() {
try {
try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
} catch (Exception ex) {
}
connection = (Connection) DriverManager.getConnection(url + schema, uname, password);
} catch (Exception ex) {
}
return connection;
}
public void closeConneciton() {
try {
connection.close();
} catch (Exception ex) {
}
}
}
/* Servlet */
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
DetailsBean db = new DetailsBean();
String name = request.getParameter("name");
String countryname = request.getParameter("countryname");
String statename = request.getParameter("statename");
db.setName(name);
db.setCountry(countryname);
db.setState(statename);
DetailsManager dm = new DetailsManager();
String result = dm.insertDetailsManager(db);
if (result.equals("true")) {
/* RequestDispatcher rd = request.getRequestDispatcher("StateDetails.jsp?name=" + db.getName());
rd.forward(request, response);*/
response.sendRedirect("StateDetails.jsp");
} else {
out.print(result);
}
/* DAO */
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import com.mysql.jdbc.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DetailsDAO {
private Connection connection;
private PersonalConnection con;
public DetailsDAO() {
con = new PersonalConnection();
}
public String insertRecordDAO(DetailsBean db) {
int rowcount=0;
try {
connection=con.openConnection();
String query = "INSERT INTO personal_details(user_name,country_name,state_name) values(?,?,?);";
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, db.getName());
ps.setString(2, db.getCountry());
ps.setString(3, db.getState());
rowcount = ps.executeUpdate();
if(rowcount==1)
{
return "true";
}
else
{
return "false";
}
} catch (Exception ex) {
return ex+"";
} finally {
con.closeConneciton();
}
}
public ResultSet getGetails() throws Exception
{
connection=con.openConnection();
String query="SELECT * from state_details";
PreparedStatement ps=connection.prepareStatement(query);
ResultSet rs=ps.executeQuery();
return rs;
}
}
/*Manager*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.ArrayList;
public class DetailsManager {
DetailsDAO detdao = new DetailsDAO();
public String insertDetailsManager(DetailsBean db) {
String rowcount = detdao.insertRecordDAO(db);
return rowcount;
}
}
/*Bean*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
public class DetailsBean {
private String name;
private String country;
private String state;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}