当前位置: 技术问答>java相关
高手请帮忙,java如何与mysql通信的问题?
来源: 互联网 发布时间:2015-08-04
本文导语: 我的读取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
|
java.sql.SQLException: Server configuration denies access to data source
从这句话来看,好像是你的用户在MYSQL中没有正确的权限!!!
将select 等权限加上.
从这句话来看,好像是你的用户在MYSQL中没有正确的权限!!!
将select 等权限加上.
|
嘿嘿,看看mysql库中的user表,设置你的用户访问权限。