发个基础jdbc

public class JDBCTest {

public static void main(String[] args) {


// �������
String driver = "com.mysql.jdbc.Driver";

// URLָ��Ҫ���ʵ���ݿ���scutcs
String url = "jdbc:mysql://127.0.0.1:3306/test";

// MySQL����ʱ���û���
String user = "root";

// MySQL����ʱ������
String password = "root";
ResultSetMetaData rsmd = null;
try {


// ���������
Class.forName(driver);

// l����ݿ�
Connection conn = DriverManager.getConnection(url, user, password);

if (!conn.isClosed())
System.out.println("Succeeded connecting to the Database!");

// statement��4ִ��SQL���
Statement statement = conn.createStatement();

// Ҫִ�е�SQL���
String sql = "select * from teacher";

// ���
ResultSet rs = statement.executeQuery(sql);

rsmd = rs.getMetaData();
int colunmCount = rsmd.getColumnCount();
for(int i = 0;i<colunmCount;i++){
String columnName = rsmd.getColumnName(i+1); //��һ������
System.out.print(columnName+"\t");
}

System.out.println();


//String name = null;

int teacher_id = 0;
String teacher_name = null;
String teacher_phone = null;
String teacher_email = null;
while (rs.next()) {

teacher_id = rs.getInt("teacher_id");
teacher_name = rs.getString("teacher_name");
teacher_phone = rs.getString("teacher_phone");
teacher_email = rs.getString("teacher_email");
System.out.println(teacher_id+"\t"+"\t"+teacher_name+"\t"+teacher_phone+"\t"+teacher_email);
}

rs.close();
conn.close();

} catch (ClassNotFoundException e) {

System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}
}
}

//测试下

public class TestInsert {

/**
* @param args
* @throws ClassNotFoundException
* @throws SQLException
* @throws UnsupportedEncodingException
*/
public static void main(String[] args) throws ClassNotFoundException, SQLException, UnsupportedEncodingException {
// TODO Auto-generated method stub

Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://" +
"localhost:3306/test", "root", "root");
// 关闭事务自动提交
con.setAutoCommit(false);

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SS");
TimeZone t = sdf.getTimeZone();
t.setRawOffset(0);
sdf.setTimeZone(t);
Long startTime = System.currentTimeMillis();

String aa = "中国";
String bb = new String(aa.getBytes(),"gb2312");
PreparedStatement pst = (PreparedStatement) con.prepareStatement("insert into test_insert values (?,'"+bb+"')");
for (int i = 1; i < 101; i++) {
pst.setInt(1, i);
// 把一个SQL命令加入命令列表
pst.addBatch();
}
// 执行批量更新
pst.executeBatch();
// 语句执行完毕,提交本事务
con.commit();

Long endTime = System.currentTimeMillis();
System.out.println("用时:" + sdf.format(new Date(endTime - startTime)));

pst.close();
con.close();

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值