请教JAVA如何连接MYSQL的问题

lg_Mp5 2006-02-12 12:52:39
网上差了点资料,按照说明照着做了,JSP运行没问题,但是运行JAVA程序的话就问题来了
...全文
322 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lg_Mp5 2006-02-12
  • 打赏
  • 举报
回复
那位高手有这方面的源码传上来让我学习下,谢谢了啊
lg_Mp5 2006-02-12
  • 打赏
  • 举报
回复
com.mysql.jdbc.Driver是不合法的驱动
PoemCode(诗意代码之魔数)所说的是正确的,我在JSP里面已经可以完成。但是在JAVA程序里面就会报没有找到合适的驱动。这是为什么?我在CLASSPATH里面已经将mysql-connector-java-3.1.12加进去了。
PoemCode 2006-02-12
  • 打赏
  • 举报
回复
String connect="jdbc:mysql://localhost/fdb_product?user=root";
????
------------------------------------------------------------------
String userName="root";
String password="";
------------------------------------------------------------------
看出来了吧
执假以为真 2006-02-12
  • 打赏
  • 举报
回复
报什么错,说说撒
PoemCode 2006-02-12
  • 打赏
  • 举报
回复
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加入进去。
PoemCode 2006-02-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;
}
}
}

}


lg_Mp5 2006-02-12
  • 打赏
  • 举报
回复
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;
}
}
xiaoxujsj 2006-02-12
  • 打赏
  • 举报
回复
把代码贴出来撒

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧