自己写的一个简单的实例
/**
*
*/
package zhtech;
import java.io.File;
import java.io.IOException;
import java.sql.*;
/**
* 数据库的连接,增删改查的实现
* @author Administrator
*
*/
public class DBManager {
public static void main(String agrs[]){
//TODO 自动生成方法存根
String strPath="";//当前程序根路径
try{
File f=new File(".");
strPath=f.getCanonicalPath();
}catch(IOException e){
System.out.println(e.toString());
}
String username="";
String password="";
Connection con;
Statement stmt;
ResultSet rs;
try{
//连接数据库
Class.forName( "org.gjt.mm.mysql.Driver" );
con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", null);
stmt=con.createStatement();
rs=stmt.executeQuery("select*from Student");//查询
while(rs.next()){
System.out.println(rs.getString("username")+"\n");
}
//stmt.executeUpdate("insert into student(username,password) value('xiaolong','123456')");//添加
stmt.executeUpdate("delete from student where ID=3 or ID=5 or ID=6 or ID=7");//删除
stmt.executeUpdate("update student set username='chen' where ID=4");//修改
rs.close();
stmt.close();
con.close();
}catch(Exception e){
System.out.println(e.toString());
}
}
}
/**
*
*/
package zhtech;
import java.io.File;
import java.io.IOException;
import java.sql.*;
/**
* 数据库的连接,增删改查的实现
* @author Administrator
*
*/
public class DBManager {
public static void main(String agrs[]){
//TODO 自动生成方法存根
String strPath="";//当前程序根路径
try{
File f=new File(".");
strPath=f.getCanonicalPath();
}catch(IOException e){
System.out.println(e.toString());
}
String username="";
String password="";
Connection con;
Statement stmt;
ResultSet rs;
try{
//连接数据库
Class.forName( "org.gjt.mm.mysql.Driver" );
con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", null);
stmt=con.createStatement();
rs=stmt.executeQuery("select*from Student");//查询
while(rs.next()){
System.out.println(rs.getString("username")+"\n");
}
//stmt.executeUpdate("insert into student(username,password) value('xiaolong','123456')");//添加
stmt.executeUpdate("delete from student where ID=3 or ID=5 or ID=6 or ID=7");//删除
stmt.executeUpdate("update student set username='chen' where ID=4");//修改
rs.close();
stmt.close();
con.close();
}catch(Exception e){
System.out.println(e.toString());
}
}
}