jdbc 纯JAVA代码数据库连接...
jdbc 纯JAVA数据库连接...怎么连?
没这方面的书.网上找了哈,那些人也解释的比较粗糙.朋友给个解释啊.只要纯JAVA代码的数据库连接.给个例子就行.谢谢.
------解决方案--------------------jdbc 连接sql server的教程、配置、例子、注意事项和各种使用的例子
http://msdn2.microsoft.com/zh-cn/library/ms378749.aspx
------解决方案-------------------- import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import
java.sql.SQLException;
import java.sql.Statement;
public class Dao {
//属性
private String IP;
private String dbString;
private String dbMing;
private String yongHuMing;
private String yongHuMima;
private String url;
private Connection conn ;
//功能
public Dao()
{
IP= "localhost ";
dbString= "com.microsoft.jdbc.sqlserver.SQLServerDriver ";
dbMing= "saolei ";
yongHuMing= "saolei ";
yongHuMima= "saolei ";
url= "jdbc:microsoft:sqlserver:// "+IP+ ":1433;DatabaseName= "+dbMing+ " ";
jiaZaiQuDong();
jianLiLianJie();
}
private void jiaZaiQuDong()
{
try
{
Class.forName(dbString);
}
catch (
ClassNotFoundException e)
{
e.printStackTrace();
}
}
private void jianLiLianJie()
{
try
{
conn = DriverManager.getConnection(url,yongHuMing,yongHuMima);
}
catch (
SQLException e)
{
e.printStackTrace();
}
}
public Statement shengChengStmt()
{
Statement stmt=null;
try
{
stmt = conn.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE ,ResultSet.CONCUR_READ_ONLY );
}
catch (SQLException e)
{
e.printStackTrace();
}
return stmt;
}
//-------------表数据---------------------------------------------------
public ResultSet query(String sql)
{
//System.out.println(sql);
Statement stmt=this.shengChengStmt();
ResultSet rs=null;
try
{
rs = stmt.executeQuery(sql);
}
catch (SQLException e)
{
e.printStackTrace();
}
return rs;
}
public int update(String sql)
{
Statement stmt=this.shengChengStmt();
int tiao=0;
try
{
tiao = stmt.executeUpdate(sql);
}
catch (SQLException e)
{
e.printStackTrace();
}
return tiao;
}
}
------解决方案--------------------原文在这里 http://whlustb.blog.com.cn/archives/2006/1544744.shtml
Step1:可在http://www.mysql.com/products/connector-j/index.html下载MySQL JDBC驱动程序mysql-connector-java-*.jar,并加入到ClassPath下面.
Step2:注册JDBC驱动程序
try {
Class.forName( "com.mysql.jdbc.Driver ");
}
catch(Class
NotFoundException e) {
System.out.println( "找不到驱动程序 ");
}
Step3:提供JDBC URL
jdbc:mysql://主机名:端口号/数据库名?user=***&password=***&useUnicode=true&characterEncoding=UTF8
端口号:MySQL的默认值是3306
useUnicode,characterEncoding:如果要存取中文,则必须使用,表明是否使用Unicode,并指定编码方式.
Step4:从DriverManager取得Connection
可以直接将JDBC URL传入DriverManager.getConnection()得到Connection对象,如:
try {
String url = "jdbc:mysql://localhost:3306/GUESTBOOK?user=caterpillar&password=123456 ";