首页 > 数据库技术 > 详细

JDBC编程示例

时间:2017-04-29 14:46:07      阅读:257      评论:0      收藏:0      [点我收藏+]

package com.lovo.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import com.lovo.bean.ClassBean;

public class TestDML {

public static void main(String[] args) {
String className = JOptionPane.showInputDialog("请输入班级名字");
String teacherName = JOptionPane.showInputDialog("请输入班主任名字");

//数据库操作步骤:
//1、加载驱动---告诉驱动管理器我们将使用哪一个数据库的驱动包
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//2、操作JDBC API完成数据库动作
//2-1、获取连接
Connection con = null;
try {
//url--统一资源定位符----样式:协议://ip地址:端口号/服务
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test134" +
"?useUnicode=true&characterEncoding=utf8&useSSL=false", "root", "root");
//2-2、书写SQL语句---字符串拼接、
String sql = "insert into t_class(f_classname,f_teacher) values(‘"+className+"‘,‘"+teacherName+"‘)";
//2-3、获取语句对象---Statement对象
Statement state = con.createStatement();
//2-4、执行语句对象---所有的DML语句,全部执行executeUpdate()方法
int row = state.executeUpdate(sql);//返回的int代表影响了多少行!
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
//2-5、关闭连接
if(con != null){
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}










}
}

JDBC编程示例

原文:http://www.cnblogs.com/fengshaolingyun/p/6785123.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!