JAVA 连接MYSQL

今天我给大家介绍一下JAVA 连接 MYSQL:
     我都不教你怎么装MYSQL了,网上我看了很多要下载JDBC我也下载了,和配置了CLASSPATH 但是还是不能用。后来我发现不需要配置了。直接COPY mysql-connector-java-5.0.7-bin.jar
D:\Program Files\Java\jdk1.6.0_02\jre\lib\ext\mysql-connector-java-5.0.7-bin.jar
所以,我希望连接的大家不要走弯路。
 这是我写的程序:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class test {
public static void main(String[] args) {
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/demo?user=root&password=billsxm" ;
// demo is datebase name
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement();
String sql="select * from my_table";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
System.out.println(rs.getString("name"));
}
} catch (Exception e) {
e.printStackTrace();
}


}
}

posted on 2007-12-29 22:38 bill 阅读(3605) 评论(3)  编辑  收藏

评论

# re: JAVA 连接MYSQL[未登录] 2007-12-29 22:58 xmlspy

spring JdbcTemplate is best  回复  更多评论   

# re: JAVA 连接MYSQL 2007-12-30 18:56 梁文政

what's the meaning of "driver is not necessary"?  回复  更多评论   

# re: JAVA 连接MYSQL 2007-12-31 10:21 凌晨风

这是一个标准的Java连接MYsql数据库的步骤,楼主写的几乎是不能用的,一个标准的连接是要关闭资源........由于没有找到合适的源码,所以就找了这个这个日期处理的源码贴上来。
package JDBC;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
/**
*
* @author oakertree
*
*/
public class TestJDBCDate {
public static void main(String[] args) {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
// new com.mysql.jdbc.Driver();
con = DriverManager.getConnection("jdbc:mysql://localhost:3307/test", "root", "admini");
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM time");
while(rs.next()) {
/*
Date d = rs.getDate("date");
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
System.out.println(sdf.format(d));
*/

/*
Time t = rs.getTime("time");
SimpleDateFormat sdf = new SimpleDateFormat("HH时mm分ss秒");
System.out.println(sdf.format(t));
*/

Date d = rs.getDate("datetime");
Time t = rs.getTime("datetime");
SimpleDateFormat sdfdate = new SimpleDateFormat("yyyy年MM月dd日");
SimpleDateFormat sdftime = new SimpleDateFormat("HH时mm分ss秒");
System.out.println(sdfdate.format(d) + sdftime.format(t));

/*
Timestamp dt = rs.getTimestamp("datetime");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
System.out.println(sdf.format(dt));
*/

//得到日期中的月
/*
Calendar c = Calendar.getInstance();
c.setTime(d);
System.out.println(c.get(Calendar.MONTH));
*/
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(rs != null) {
rs.close();
rs = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
if(con != null) {
con.close();
con = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
/*
+----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| date | date | YES | | NULL | |
| time | time | YES | | NULL | |
| datetime | datetime | YES | | NULL | |
+----------+----------+------+-----+---------+-------+
*/
  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2007年12月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

导航

统计

常用链接

留言簿(1)

随笔分类(2)

随笔档案(4)

相册

搜索

最新评论

阅读排行榜

评论排行榜