当前位置: 技术问答>java相关
高手请帮忙,我的class连不上mysql数据库?
来源: 互联网 发布时间:2015-08-07
本文导语: 我的读取connection的java文件如下: import java.sql.*; import java.io.*; import java.util.Properties; import com.mysql.jdbc.Driver; //import org.gjt.mm.mysql.Driver;//这个驱动程序我也试过了,抛出的错误一样 public class MysqlConn implements Serializab...
我的读取connection的java文件如下:
import java.sql.*;
import java.io.*;
import java.util.Properties;
import com.mysql.jdbc.Driver;
//import org.gjt.mm.mysql.Driver;//这个驱动程序我也试过了,抛出的错误一样
public class MysqlConn implements Serializable{
public MysqlConn() {
}
private static String MysqlJdbcDrv ="com.mysql.jdbc.Driver";
//private static String MysqlJdbcDrv = "org.gjt.mm.mysql.Driver";
private static String MysqlJdbcURL = "jdbc:mysql://localhost:3306/caac?user=root&password=12345678";
public String getMysqlJdbcDrv() {
return MysqlJdbcDrv;
}
public String getMysqlJdbcURL() {
return MysqlJdbcURL;
}
public void setMysqlJdbcURL(/tech-qa-java/String MysqlJdbcURL/index.html) {
this.MysqlJdbcURL = MysqlJdbcURL;
}
public void setMysqlJdbcDrv(String MysqlJdbcDrv) {
this.MysqlJdbcDrv = MysqlJdbcDrv;
}
public static Connection getConnection(boolean isappserver) {
if(!isappserver) {
System.out.println("There is not any application server");
return null;
}
Connection con = null;
try {
Class.forName(MysqlJdbcDrv).newInstance();
}catch(Exception e){
e.printStackTrace();
return null;
}
try {
con = DriverManager.getConnection(MysqlJdbcURL);//在这里就抛错了
}catch(Exception e){
e.printStackTrace();
return null;
}
return con ;
}
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
ois.defaultReadObject();
}
private void writeObject(ObjectOutputStream oos) throws IOException {
oos.defaultWriteObject();
}
}
读取查询结果文件的函数如下,这是一个application可以运行,我调用了下面的方法.
public News getNews(short newsid){
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
Statement st = null;
String sql = null;
try {
con = MysqlConn.getConnection(true);
sql = "select newid,title,content1,picture,developtime,developbranch,createtime,articledes,ptutoindex from News where newsid=?";
System.out.println("sql="+sql);
pst.setShort(1,newsid);
News news = null;
if(rs!=null && rs.next()){
news = new News();
if (con==null) return null;
news.Newsid = rs.getShort("newid");
news.content = new String(rs.getBlob("content1").getBytes(0,2));
news.Picture = rs.getString("picture");
news.Developtime = rs.getTimestamp("developtime");
news.Developbranch = rs.getString("developbranch");
news.Createtime = rs.getDate("createtime");
news.Articledes = rs.getString("articledes");
if(rs.getShort("puttoindex")==1)
news.Puttoindex=true;
else news.Puttoindex=false;
rs.close(); rs = null ;
pst.close(); pst = null ;
}
return news;
}catch(Exception e){e.printStackTrace();return null;}
finally {
try { rs.close(); } catch(SQLException ne) { ne.printStackTrace(); }
try { pst.close(); } catch(SQLException ne) { ne.printStackTrace(); }
try { con.close(); } catch(SQLException ne) { ne.printStackTrace(); }
}
}
错误显示:
java.sql.SQLException: Server configuration denies access to data source
at com.mysql.jdbc.MysqlIO.init(MysqlIO.java:446)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:1605)
at com.mysql.jdbc.Connection.connectionInit(Connection.java:1056)
at com.mysql.jdbc.Driver.connect(Driver.java:297)
at java.sql.DriverManager.getConnection(DriverManager.java:512)
at java.sql.DriverManager.getConnection(DriverManager.java:193)
at com.touch.MysqlConn.getConnection(MysqlConn.java:88)
at com.touch.NewsConnBean.getNews(NewsConnBean.java:63)
at website.TestNews.getNews(TestNews.java:34)
at website.TestNews.main(TestNews.java:25)
我的配置如下:
mysql-3.23.51,已经起动了服务器,
我的mysql jdbc包
org.gjt.mm.mysql.Driver----mm.mysql-2.0.4-bin.jar
com.mysql.jdbc.Driver---Connection/J 的mysql-connector-java-3.0.0-bin.jar
两个驱动程序都已经尝试过了,都抛出同样的错误,请问高手,
(1)知道java如何与mysql通信吗?
(2)我的驱动程序包正确吗?如果不正确的话可以告诉我应该是哪个jar呢?可以给我发一个吗?jacquiyan@21cn.com
import java.sql.*;
import java.io.*;
import java.util.Properties;
import com.mysql.jdbc.Driver;
//import org.gjt.mm.mysql.Driver;//这个驱动程序我也试过了,抛出的错误一样
public class MysqlConn implements Serializable{
public MysqlConn() {
}
private static String MysqlJdbcDrv ="com.mysql.jdbc.Driver";
//private static String MysqlJdbcDrv = "org.gjt.mm.mysql.Driver";
private static String MysqlJdbcURL = "jdbc:mysql://localhost:3306/caac?user=root&password=12345678";
public String getMysqlJdbcDrv() {
return MysqlJdbcDrv;
}
public String getMysqlJdbcURL() {
return MysqlJdbcURL;
}
public void setMysqlJdbcURL(/tech-qa-java/String MysqlJdbcURL/index.html) {
this.MysqlJdbcURL = MysqlJdbcURL;
}
public void setMysqlJdbcDrv(String MysqlJdbcDrv) {
this.MysqlJdbcDrv = MysqlJdbcDrv;
}
public static Connection getConnection(boolean isappserver) {
if(!isappserver) {
System.out.println("There is not any application server");
return null;
}
Connection con = null;
try {
Class.forName(MysqlJdbcDrv).newInstance();
}catch(Exception e){
e.printStackTrace();
return null;
}
try {
con = DriverManager.getConnection(MysqlJdbcURL);//在这里就抛错了
}catch(Exception e){
e.printStackTrace();
return null;
}
return con ;
}
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
ois.defaultReadObject();
}
private void writeObject(ObjectOutputStream oos) throws IOException {
oos.defaultWriteObject();
}
}
读取查询结果文件的函数如下,这是一个application可以运行,我调用了下面的方法.
public News getNews(short newsid){
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
Statement st = null;
String sql = null;
try {
con = MysqlConn.getConnection(true);
sql = "select newid,title,content1,picture,developtime,developbranch,createtime,articledes,ptutoindex from News where newsid=?";
System.out.println("sql="+sql);
pst.setShort(1,newsid);
News news = null;
if(rs!=null && rs.next()){
news = new News();
if (con==null) return null;
news.Newsid = rs.getShort("newid");
news.content = new String(rs.getBlob("content1").getBytes(0,2));
news.Picture = rs.getString("picture");
news.Developtime = rs.getTimestamp("developtime");
news.Developbranch = rs.getString("developbranch");
news.Createtime = rs.getDate("createtime");
news.Articledes = rs.getString("articledes");
if(rs.getShort("puttoindex")==1)
news.Puttoindex=true;
else news.Puttoindex=false;
rs.close(); rs = null ;
pst.close(); pst = null ;
}
return news;
}catch(Exception e){e.printStackTrace();return null;}
finally {
try { rs.close(); } catch(SQLException ne) { ne.printStackTrace(); }
try { pst.close(); } catch(SQLException ne) { ne.printStackTrace(); }
try { con.close(); } catch(SQLException ne) { ne.printStackTrace(); }
}
}
错误显示:
java.sql.SQLException: Server configuration denies access to data source
at com.mysql.jdbc.MysqlIO.init(MysqlIO.java:446)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:1605)
at com.mysql.jdbc.Connection.connectionInit(Connection.java:1056)
at com.mysql.jdbc.Driver.connect(Driver.java:297)
at java.sql.DriverManager.getConnection(DriverManager.java:512)
at java.sql.DriverManager.getConnection(DriverManager.java:193)
at com.touch.MysqlConn.getConnection(MysqlConn.java:88)
at com.touch.NewsConnBean.getNews(NewsConnBean.java:63)
at website.TestNews.getNews(TestNews.java:34)
at website.TestNews.main(TestNews.java:25)
我的配置如下:
mysql-3.23.51,已经起动了服务器,
我的mysql jdbc包
org.gjt.mm.mysql.Driver----mm.mysql-2.0.4-bin.jar
com.mysql.jdbc.Driver---Connection/J 的mysql-connector-java-3.0.0-bin.jar
两个驱动程序都已经尝试过了,都抛出同样的错误,请问高手,
(1)知道java如何与mysql通信吗?
(2)我的驱动程序包正确吗?如果不正确的话可以告诉我应该是哪个jar呢?可以给我发一个吗?jacquiyan@21cn.com
|
1、将localhost改为127.0.0.1试, 我曾经有一次就这问题。
2。你建立一个新的用户,记住要分配它localhost登陆的权限,在试!
然后我也不知道了!
2。你建立一个新的用户,记住要分配它localhost登陆的权限,在试!
然后我也不知道了!
|
一定是数据库访问权限设置问题,进入mysql后建立一个新用户并赋予权限就应该可以了!
如:
grant all on databaseName.* to yanyan identified by "123456";
flush privileges;
就把databaseName数据库的权限赋给了yanyan用户,并刷新。
然后修改
private static String MysqlJdbcURL = "jdbc:mysql://localhost:3306/caac?user=yanyan&password=123456";
就可以了!
如:
grant all on databaseName.* to yanyan identified by "123456";
flush privileges;
就把databaseName数据库的权限赋给了yanyan用户,并刷新。
然后修改
private static String MysqlJdbcURL = "jdbc:mysql://localhost:3306/caac?user=yanyan&password=123456";
就可以了!
|
Driver driver = (Driver) Class.forName( ServerTreeNode.driverName ).newInstance();
DriverManager.registerDriver( driver );
connection = DriverManager.getConnection( connURL, userID, passWord);
试一下,我一直用的就是那个org.gjt包,没问题的。而且也是2.0.4最好用了:)
DriverManager.registerDriver( driver );
connection = DriverManager.getConnection( connURL, userID, passWord);
试一下,我一直用的就是那个org.gjt包,没问题的。而且也是2.0.4最好用了:)
|
这就不是jdbc的问题了!是你数据库设置问题!
你试试看在mysql下用root能否登陆!或者新建一个database用户
你试试看在mysql下用root能否登陆!或者新建一个database用户
|
String driver ="org.gjt.mm.mysql.Driver";
Connection conn = null;
Statement stmt= null;
ResultSet rs = null;
String sql="select * from test";
Class.forName(driver);
conn = DriverManager.getConnection("jdbc:mysql://localhost/caac?user=root&password=12345678");
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
你试试看!!!
Connection conn = null;
Statement stmt= null;
ResultSet rs = null;
String sql="select * from test";
Class.forName(driver);
conn = DriverManager.getConnection("jdbc:mysql://localhost/caac?user=root&password=12345678");
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
你试试看!!!
|
很可能是你的环境配置问题了,
看在是本家的份上,我的MSN是sharetop@hotmail.com
看在是本家的份上,我的MSN是sharetop@hotmail.com
|
请问你在jbuilder中运行吗?如果是,请看一下project->project properties->path->Required Libraries下有没加入你的mysql?
有可能是这个问题吧
有可能是这个问题吧