做web的时候放入到lib下面就可以了、、、//一个小例子//运行命令://java -cp .;mysql-connector-java-3.1.10-bin.jar InsertBlobInMysqlimport java.io.File;import java.io.FileInputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;/在mysql数...
java程序在控制台下怎样连接MySQL数据库,驱动程序怎么设置啊?
build path 把Mysql的驱动包加进环境来,这个jar 文件可以在
http://dev.mysql.com/downloads/上面下载一个conector 按你的MySql的版本来下。最好把源文件和JAR文件都下载下来(Source and Binaries),有空可以看一下它的源码,注意下载下来解压后的哪个JAR文件才是我们用的。WEB应用我们把它入在web-inf 下的lib文件夹就行了(不用再build path,也不用配环境,当然你用JNDI 或其它的DataSource的话除外),然后就是在程序中使用这个驱动了。QQ526220472009-08-20
做web的时候放入到lib下面就可以了、、、、、、2009-08-20
//一个小例子
//运行命令:
//java -cp .;mysql-connector-java-3.1.10-bin.jar InsertBlobInMysql
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
/**
*在mysql数据库里成功测试,并且发现mysql的blob数据库不支持存储图片,只支持
*65535字节以下的本本数据存储。不过其他的大型数据库是支持储存图片的.
*/
public class InsertBlobInMysql {
public static void main(String[] args) {
try {
File f = new File("C:\\bsmain_runtime.log");
long length = f.length();
FileInputStream fis = new FileInputStream("C:\\bsmain_runtime.log");
byte[] imageBytes = new byte[(int) length];
int byteLength = fis.read(imageBytes, 0, (int) length);
ByteArrayInputStream bais = new ByteArrayInputStream(imageBytes);
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost/test", "root", "12345");
PreparedStatement pstmt = null;
/*
create table mypicture
( name varchar(20),
image blob );
*/
pstmt = con
.prepareStatement("insert into mypicture(name,image) values(?,?)");
pstmt.setString(1, "001");
pstmt.setBinaryStream(2, bais, byteLength);
pstmt.executeUpdate();
System.out.println("file length:" + length);
System.out.println("byte length:" + byteLength);
System.out.println("插入成功.");
} catch (Exception e) {
e.printStackTrace();
}
}
}2009-08-20