请教JAVA如何连接MYSQL的问题 网上差了点资料,按照说明照着做了,JSP运行没问题,但是运行JAVA程序的话就问题来了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package pag;import java.sql.*;public class conn{ String driverName="com.mysql.jdbc.Driver"; String connect="jdbc:mysql://localhost/fdb_product?user=root"; String userName="root"; String password=""; Connection con=null; ResultSet rs=null; public void con(){ try{ Class.forName(driverName); }catch(java.lang.ClassNotFoundException e){ System.err.println(e.getMessage()); } } public ResultSet executeQuery(String sql){ try{ con=DriverManager.getConnection(connect); Statement stmt=con.createStatement(); rs=stmt.executeQuery(sql); }catch(SQLException ex){ System.err.println(ex.getMessage()); } return rs; }} String connect="jdbc:mysql://localhost/fdb_product?user=root";????------------------------------------------------------------------String userName="root";String password="";------------------------------------------------------------------看出来了吧 com.mysql.jdbc.Driver是不合法的驱动 PoemCode(诗意代码之魔数)所说的是正确的,我在JSP里面已经可以完成。但是在JAVA程序里面就会报没有找到合适的驱动。这是为什么?我在CLASSPATH里面已经将mysql-connector-java-3.1.12加进去了。 /* * SQLController.java * * Created on 2006年1月16日, 下午1:33 */package sqlmvc;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.sql.Statement;import java.util.Vector;/** * * @author poco */public class SQLController { /** Creates a new instance of SQLController */ public SQLController() { } public Connection con; public DriverManager driver; public Statement state; public Connection getCon() { if(con == null){ try { Class.forName("com.mysql.jdbc.Driver"); System.out.println("load the driver"); con = DriverManager.getConnection("jdbc:mysql://localhost/MVC?user=poemcode&password=aaaa&useUnicode=true&characterEncoding=UTF-8"); System.out.println("try to get Connetion"); if(con == null) System.out.println("failed to get Connection."); else System.out.println("get Conection"); } catch (ClassNotFoundException ex) { System.err.println("Can't find the driver of MySql"); ex.printStackTrace(); } catch (SQLException ex) { System.out.println("SQLState: " + ex.getSQLState() + " ErrorCode: " + ex.getErrorCode()); ex.printStackTrace(); } } return con; } public Vector cache; public int colCount; public String[] headers; public void doQuery(String info) { cache = new Vector(); try { state = getCon().createStatement(); System.out.println("create Statement"); result = state.executeQuery(info); System.out.println("get ResultSet"); if(state.execute(info)){ result = state.getResultSet(); rsmd = result.getMetaData(); colCount = rsmd.getColumnCount(); headers = new String[colCount]; for(int t = 1; t <= colCount; t++) headers[t-1] = rsmd.getColumnName(t); while(result.next()){ String record[] = new String[colCount]; for(int i = 0; i < colCount; i++) record[i] = result.getString(i+1); cache.addElement(record); } //fireTableChanged(null); } } catch (SQLException ex) { System.out.println("Can't create Statement."); System.err.println("SQLState: " + ex.getSQLState() + " ErrorCode: " + ex.getErrorCode()); ex.printStackTrace(); }finally { if (result != null) { try { result.close(); System.out.println("ResultSet close."); } catch (SQLException sqlEx) { sqlEx.printStackTrace(); } result = null; } if (state != null) { try { state.close(); System.out.println("Statement close."); } catch (SQLException sqlEx) { sqlEx.printStackTrace(); } state = null; } } } public ResultSet result; public ResultSetMetaData rsmd; public void doDelete(String info) { try { state = getCon().createStatement(); System.out.println("create Statement."); state.execute(info); System.out.println("delete the info."); } catch (SQLException ex) { System.out.println("Can't create Statement."); System.err.println("SQLState: " + ex.getSQLState() + " ErrorCode: " + ex.getErrorCode()); ex.printStackTrace(); }finally { if (result != null) { try { result.close(); System.out.println("ResultSet close."); } catch (SQLException sqlEx) { sqlEx.printStackTrace(); } result = null; } if (state != null) { try { state.close(); System.out.println("Statement close."); } catch (SQLException sqlEx) { sqlEx.printStackTrace(); } state = null; } } } public void doInsert(String info) { try { state = getCon().createStatement(); System.out.println("create Statement."); state.execute(info); System.out.println("insert the info."); } catch (SQLException ex) { System.out.println("Can't create Statement."); System.err.println("SQLState: " + ex.getSQLState() + " ErrorCode: " + ex.getErrorCode()); ex.printStackTrace(); }finally { if (result != null) { try { result.close(); System.out.println("ResultSet close."); } catch (SQLException sqlEx) { sqlEx.printStackTrace(); } result = null; } if (state != null) { try { state.close(); System.out.println("Statement close."); } catch (SQLException sqlEx) { sqlEx.printStackTrace(); } state = null; } } } public void doUpdate(String info) { try { state = getCon().createStatement(); System.out.println("create Statement."); state.execute(info); System.out.println("insert the info."); } catch (SQLException ex) { System.out.println("Can't create Statement."); System.err.println("SQLState: " + ex.getSQLState() + " ErrorCode: " + ex.getErrorCode()); ex.printStackTrace(); }finally { if (result != null) { try { result.close(); System.out.println("ResultSet close."); } catch (SQLException sqlEx) { sqlEx.printStackTrace(); } result = null; } if (state != null) { try { state.close(); System.out.println("Statement close."); } catch (SQLException sqlEx) { sqlEx.printStackTrace(); } state = null; } } } } String connect="jdbc:mysql://localhost/fdb_product?user=root";String userName="root";String password="";------------------------------------------------------------------String connect="jdbc:mysql://localhost/fdb_product";String userName="root";String password="";我第一次回贴的时候发现你重复了user,因此我问你看明白没有(误会了你的意思)Connection con = Drivermanager.getConnection(connect,userName,password);或者形如我的例子中所采用形式:con=DriverManager.getConnection("jdbc:mysql://localhost/MVC?user=poemcode&password=aaaa");注:这里MVC是数据库名称,用户名为poemcode,密码为aaaa。此例为我在上学期课程设计中的代码,测试通过。在你的回贴中“我在CLASSPATH里面已经将mysql-connector-java-3.1.12加进去了。”,真正的应该是把mysql-connector-java-3.1.12-bin.jar加入进去。 java 远程通信 如何取name值 Collection c = new HashSet();出错,为什么? 急,写不进数据库 jsp里默认如何选择combo中的一个项 关于倒出内容到csv格式文件的问题 请问编写JSP是否需要另外下载Servlet包 ? ba 请教一个WT! 如何和浏览器产生一个socket通讯???我还是没有解决 希望大家再帮帮忙 请问各位大虾,在Applet中如何缩放图片? InvocationTargetException异常是什么异常 运用ZipOutputStream??
import java.sql.*;
public class conn{
String driverName="com.mysql.jdbc.Driver";
String connect="jdbc:mysql://localhost/fdb_product?user=root";
String userName="root";
String password="";
Connection con=null;
ResultSet rs=null;
public void con(){
try{
Class.forName(driverName);
}catch(java.lang.ClassNotFoundException e){
System.err.println(e.getMessage());
}
}
public ResultSet executeQuery(String sql){
try{
con=DriverManager.getConnection(connect);
Statement stmt=con.createStatement();
rs=stmt.executeQuery(sql);
}catch(SQLException ex){
System.err.println(ex.getMessage());
}
return rs;
}
}
????
------------------------------------------------------------------
String userName="root";
String password="";
------------------------------------------------------------------
看出来了吧
PoemCode(诗意代码之魔数)所说的是正确的,我在JSP里面已经可以完成。但是在JAVA程序里面就会报没有找到合适的驱动。这是为什么?我在CLASSPATH里面已经将mysql-connector-java-3.1.12加进去了。
* SQLController.java
*
* Created on 2006年1月16日, 下午1:33
*/package sqlmvc;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;/**
*
* @author poco
*/
public class SQLController {
/** Creates a new instance of SQLController */
public SQLController() {
}
public Connection con;
public DriverManager driver;
public Statement state;
public Connection getCon() {
if(con == null){
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("load the driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/MVC?user=poemcode&password=aaaa&useUnicode=true&characterEncoding=UTF-8");
System.out.println("try to get Connetion");
if(con == null)
System.out.println("failed to get Connection.");
else
System.out.println("get Conection");
} catch (ClassNotFoundException ex) {
System.err.println("Can't find the driver of MySql");
ex.printStackTrace();
} catch (SQLException ex) {
System.out.println("SQLState: " + ex.getSQLState() + " ErrorCode: " + ex.getErrorCode());
ex.printStackTrace();
}
}
return con;
}
public Vector cache;
public int colCount;
public String[] headers;
public void doQuery(String info) {
cache = new Vector();
try {
state = getCon().createStatement();
System.out.println("create Statement");
result = state.executeQuery(info);
System.out.println("get ResultSet");
if(state.execute(info)){
result = state.getResultSet();
rsmd = result.getMetaData();
colCount = rsmd.getColumnCount();
headers = new String[colCount];
for(int t = 1; t <= colCount; t++)
headers[t-1] = rsmd.getColumnName(t);
while(result.next()){
String record[] = new String[colCount];
for(int i = 0; i < colCount; i++)
record[i] = result.getString(i+1);
cache.addElement(record);
}
//fireTableChanged(null);
}
} catch (SQLException ex) {
System.out.println("Can't create Statement.");
System.err.println("SQLState: " + ex.getSQLState() + " ErrorCode: " + ex.getErrorCode());
ex.printStackTrace();
}finally {
if (result != null) {
try {
result.close();
System.out.println("ResultSet close.");
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
}
result = null;
}
if (state != null) {
try {
state.close();
System.out.println("Statement close.");
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
}
state = null;
}
}
}
public ResultSet result;
public ResultSetMetaData rsmd; public void doDelete(String info) {
try {
state = getCon().createStatement();
System.out.println("create Statement.");
state.execute(info);
System.out.println("delete the info."); } catch (SQLException ex) {
System.out.println("Can't create Statement.");
System.err.println("SQLState: " + ex.getSQLState() + " ErrorCode: " + ex.getErrorCode());
ex.printStackTrace();
}finally {
if (result != null) {
try {
result.close();
System.out.println("ResultSet close.");
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
}
result = null;
}
if (state != null) {
try {
state.close();
System.out.println("Statement close.");
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
}
state = null;
}
}
} public void doInsert(String info) {
try {
state = getCon().createStatement();
System.out.println("create Statement.");
state.execute(info);
System.out.println("insert the info."); } catch (SQLException ex) {
System.out.println("Can't create Statement.");
System.err.println("SQLState: " + ex.getSQLState() + " ErrorCode: " + ex.getErrorCode());
ex.printStackTrace();
}finally {
if (result != null) {
try {
result.close();
System.out.println("ResultSet close.");
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
}
result = null;
}
if (state != null) {
try {
state.close();
System.out.println("Statement close.");
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
}
state = null;
}
}
} public void doUpdate(String info) {
try {
state = getCon().createStatement();
System.out.println("create Statement.");
state.execute(info);
System.out.println("insert the info."); } catch (SQLException ex) {
System.out.println("Can't create Statement.");
System.err.println("SQLState: " + ex.getSQLState() + " ErrorCode: " + ex.getErrorCode());
ex.printStackTrace();
}finally {
if (result != null) {
try {
result.close();
System.out.println("ResultSet close.");
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
}
result = null;
}
if (state != null) {
try {
state.close();
System.out.println("Statement close.");
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
}
state = null;
}
}
}
}
String userName="root";
String password="";
------------------------------------------------------------------
String connect="jdbc:mysql://localhost/fdb_product";
String userName="root";
String password="";我第一次回贴的时候发现你重复了user,因此我问你看明白没有(误会了你的意思)
Connection con = Drivermanager.getConnection(connect,userName,password);
或者形如我的例子中所采用形式:
con=DriverManager.getConnection("jdbc:mysql://localhost/MVC?user=poemcode&password=aaaa");
注:这里MVC是数据库名称,用户名为poemcode,密码为aaaa。此例为我在上学期课程设计中的代码,测试通过。
在你的回贴中“我在CLASSPATH里面已经将mysql-connector-java-3.1.12加进去了。”,真正的应该是把mysql-connector-java-3.1.12-bin.jar加入进去。