哎!第一次用mysql,就遇到这个问题,搞了两天了,哭啊!
那位高手帮忙看下?
程序结构:
数据库
代码
DBConnection.java
package com.struts.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnection {
private static Connection con = null;
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
if(null==con){
try {
con = DriverManager.getConnection("jdbc:mysql://localhost/lianxi_temp","root","root");
} catch (SQLException e) {
e.printStackTrace();
}
}
return con;
}
}
--------------------------------------------------------------------------
DMLConnection.java
package com.struts.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DMLConnection {
public String getByid(int count) {
String sql = "select * from student";
String str = "";
Connection con = DBConnection.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
rs.close();
pstmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return str;
}
}
----------------------------------------------------
工具 mysql5.0 JDK1.6 驱动包是 mysql-connector-java-5.1.6-bin.jar
----------------------------------------------------
可以帮忙 运行下看看嘛 就这两个类,
谢谢!
------解决方案--------------------
把错误信息贴出来
------解决方案--------------------
楼主把mysql的端口改成80了吗?
------解决方案--------------------
con = DriverManager.getConnection("jdbc:mysql://localhost/lianxi_temp","root","root");
改为
con = DriverManager.getConnection("jdbc:mysql://localhost:你mysql的端口号/lianxi_temp","root","root");
------解决方案--------------------
首先你的count和str没有用啊,还有这个错误感觉像是连接数据库的jar包的问题,和你的数据库版本不配吧。
------解决方案--------------------
你在pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
中间加一句pstmt.execute();试一试!
------解决方案--------------------