bufegar 2007-4-9 09:14
一个jdbc连接mysql的小例子 (转)
创建数据库
指定数据库字符集和校对规则:
mysql> create database it315 default character set gb2312 collate gb2312_chinese_ci;
进入刚创建的数据库:
mysql> use it315;
Database changed
创建表:
mysql> create table student (id int primary key,name varchar(20),address varchar(20));
写一个属性文件: jdbc_mysql.properties,此文件存储一些连接相关的信息:
[xml]
driver=com.mysql.jdbc.Driver //驱动名称
url=jdbc:mysql://localhost:3306/it315 //指定URL
user=root //用户名
password= //密码
characterEncoding=gb2312 //指定字符编码
[/xml]
写java源文件: JdbcMySql.java
[java]
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
public class JdbcMySql {
public static Properties getProperties() {
Properties props = new Properties();
InputStream is = null;
try {
is = JdbcMySql.class
.getResourceAsStream("/jdbc_mysql.properties");
props.load(is);
} catch (Exception ex) {
ex.printStackTrace();
return null;
} finally {
if (is != null)
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return props;
}
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
Properties props = getProperties();
if (props != null){
// 读取出属性文件里面的内容
String driver = props.getProperty("driver");
String url = props.getProperty("url");
String user = props.getProperty("user");
String password = props.getProperty("password");
String characterEncoding = props.getProperty("characterEncoding");
try {
Class.forName(driver);// 加载驱动
conn = DriverManager.getConnection(url + "?characterEncoding="
+ characterEncoding, user, password);// 建立连接
stmt = conn.createStatement();
String sql = "insert into student values(1,' 张三','湖南')";
stmt.executeUpdate(sql);// 执行sql语句
sql = "select * from Student";
rs = stmt.executeQuery(sql);
while (rs.next()) {// 从结果集中取出数据
System.out.print(rs.getInt(1) + "\t");
System.out.print(rs.getString(2) + "\t");
System.out.print(rs.getString(3) + "\n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {// 释放连接
try {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
}
[/java]
编译运行:
C:\ >javac JdbcMySql.java
C:\ >java JdbcMySql
1 张三 湖南
进数据库查询刚刚插入的信息:
mysql> select * from student;
+----+------+---------+
| id | name | address |
+----+------+---------+
| 1 | ??? | ?? |
+----+------+---------+
我们看到中文变成了问号.别急!退出mysql
mysql> quit;
Bye
再进去:
C:\ >mysql -uroot --default-character-set=gb2312
查询:
mysql> use it315;
Database changed
mysql> select * from student;
+----+--------+---------+
| id | name | address |
+----+--------+---------+
| 1 |张三 | 湖南 |
+----+--------+---------+
OK!一切正常了….
bufegar 2007-11-23 15:39
JDBC连接MySQL
JDBC连接MySQL
加载及注册JDBC驱动程序
Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.mysql.jdbc.Driver").newInstance();
JDBC URL 定义驱动程序与数据源之间的连接
标准语法:
<protocol(主要通讯协议)>:<subprotocol(次要通讯协议,即驱动程序名称)>:<data source identifier(数据源)>
MySQL的JDBC URL格式:
jdbc:mysql//[hostname][:port]/[dbname][?param1=value1][¶m2=value2]….
示例:jdbc:mysql://localhost:3306/sample_db?user=root&password=your_password
常见参数:
user 用户名
password 密码
autoReconnect 联机失败,是否重新联机(true/false)
maxReconnect 尝试重新联机次数
initialTimeout 尝试重新联机间隔
maxRows 传回最大行数
useUnicode 是否使用Unicode字体编码(true/false)
characterEncoding 何种编码(GB2312/UTF-8/…)
relaxAutocommit 是否自动提交(true/false)
capitalizeTypeNames 数据定义的名称以大写表示
建立连接对象
String url="jdbc:mysql://localhost:3306/sample_db?user=root&password=your_password";
Connection con = DriverManager.getConnection(url);
建立SQL陈述式对象(Statement Object)
Statement stmt = con.createStatement();
执行SQL语句
executeQuery()
String query = "select * from test";
ResultSet rs=stmt.executeQuery(query);
结果集ResultSet
while(rs.next())
{rs.getString(1);rs.getInt(2);}
executeUpdate()
String upd="insert into test (id,name) values(1001,xuzhaori)";
int con=stmt.executeUpdate(upd);
execute()