package mysqltest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.gjt.mm.mysql.Driver;
import java.io.File;
import java.io.FileInputStream;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/public class test {
/**
* JDBC URL, initialized from com.mysql.jdbc.testsuite.url
* system property, or defaults to jdbc:mysql:///test
*/ public test() {
}
public static void main(String[] args) {
Connection conn = null; try
{
File file = new File("c://dd.bmp");
FileInputStream fis = new FileInputStream(file);
Object sd = (Object)fis;
int lenth = 0;
lenth= (int)file.length();
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://192.168.238.35/mail?useUnicode=true&characterEncoding=gb2312";
String str = "小dd";
conn= DriverManager.getConnection(url,"root","");
PreparedStatement ps = conn.prepareStatement("INSERT INTO test VALUES (?)");
ps.setString(1,new String(str.getBytes("gb2312")));
// ps.setObject(2,sd);
FileInputStream dsd = (FileInputStream)sd;
//Statement stat=conn.createStatement();
// ResultSet rs=stat.executeQuery("select * from custom_folder");
// System.out.println(rs.next()) ;
ps.executeUpdate();
ps.close();
}
catch (Exception e)
{
e.printStackTrace();
System.err.println ("Cannot connect to database server");
}
finally
{
if (conn != null)
{
try
{
conn.close ();
System.out.println ("Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
} }
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.gjt.mm.mysql.Driver;
import java.io.File;
import java.io.FileInputStream;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/public class test {
/**
* JDBC URL, initialized from com.mysql.jdbc.testsuite.url
* system property, or defaults to jdbc:mysql:///test
*/ public test() {
}
public static void main(String[] args) {
Connection conn = null; try
{
File file = new File("c://dd.bmp");
FileInputStream fis = new FileInputStream(file);
Object sd = (Object)fis;
int lenth = 0;
lenth= (int)file.length();
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://192.168.238.35/mail?useUnicode=true&characterEncoding=gb2312";
String str = "小dd";
conn= DriverManager.getConnection(url,"root","");
PreparedStatement ps = conn.prepareStatement("INSERT INTO test VALUES (?)");
ps.setString(1,new String(str.getBytes("gb2312")));
// ps.setObject(2,sd);
FileInputStream dsd = (FileInputStream)sd;
//Statement stat=conn.createStatement();
// ResultSet rs=stat.executeQuery("select * from custom_folder");
// System.out.println(rs.next()) ;
ps.executeUpdate();
ps.close();
}
catch (Exception e)
{
e.printStackTrace();
System.err.println ("Cannot connect to database server");
}
finally
{
if (conn != null)
{
try
{
conn.close ();
System.out.println ("Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
} }
}
import java.io.*;public Example{
static String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
Connection connection;
Class.forName(driver);
connection = DriverManager.getConnection ("jdbc:microsoft:sqlserver://server1:1433","username","pswd");
}1.server1 是数据库机器的名字或ip
2.这是针对MS SqlServer 的,并且你要安装 “SQL Server 2000 Driver for JDBC“驱动
3.其它的数据库的话你要修改 driver 和 getConnection() 的参数,具体的网上查吧