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();
}
}
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();
}
}