您的位置:首页 > 编程语言 > Java开发

Java通过JDBC访问MYSQL示例

2007-06-24 14:12 399 查看
/**
* mysql连接数据库
* 当然首先得配置好驱动程序,保证程序可以正常操作数据库
* 驱动程序下载地址:http://mysql.ntu.edu.tw/Downloads/Connector-J/mysql-connector-java-5.0.6.zip
*/
import java.sql.*;
public class Test4 {
public Connection getConn()
{
Connection C=null;
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
C= DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=admin&useUnicode=true&characterEncoding=8859_1");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return C;
}
public static void main(String[] arg)
{
Test4 test=new Test4();
Connection conn=test.getConn();
try {
Statement st=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
st.execute("insert into test values(2,'FLB2',25)");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(conn!=null)
{
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.exit(0);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: