如何使用控制台操作数据库

使用控制台对数据库进行操作,首先,加载MySQL的数据库连接驱动
Class.forName("com.mysql.jdbc.Driver");Driver大写
然后连接数据库
Connection connect = DriverManager.getConnection(str,user,password);
String str = "jdbc:mysql://localhost:3306/myjava";//localhost可以是IP地址,3306是端口号,myjava是数据库的一个具体数据库可以包含多个表
String user = "root";//root是根目录
String password = "a123";//a123是MySQL数据库的密码
如果对数据库进行操作那么需要
Statement statement = connect.createStatement;
statement.excute("insert into course(name,id)values("java",12)");//按sql的基础语句写入你想办的是
如果想对数据库的内容进行显示,需要ResultSet集合
ResultSet resultSet = statement.excuteQuery(str);//str 为你进行的操作,比如String str = select*from course;
public class DbConnection {
String url = "jdbc:mysql://localhost:3306/myjava";
String uer = "root";
String password = "a123";
// 加载mysql数据库连接驱动
public void loadDBConnect(){
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//添加课程
public void addCourse(){
Connection connect = openConnect();//启用方法带入参数
System.out.println("请输入添加课程的称号");
String str = writeIn();
System.out.println("请输入编号");
String ID = writeIn();
Statement state;
try {

state = connect.createStatement();
state.execute("insert into course(name,id)values(\""+str+"\","+ID+")");
System.out.println("添加完成");
} catch (SQLException e) {
System.out.println("重复添加");
}finally{
shutDownConnect(connect);//方便给shutDownConnect传参
}
}
//方法2 ,添加课程
public void addCou(){
Connection connect = openConnect();
try {
PreparedStatement ps = connect.prepareStatement("insert into course(name,id)values(?,?)");
ps.setString(1,"ios");//1,指代第一个?代表name
ps.setInt(2, 18);//2,指代第二个?代表id
ps.execute();
System.out.println("添加完成");
} catch (SQLException e) {
System.out.println("重复添加");
}
}
//删除课程
public void deleteCourse(){
Connection connect = openConnect();//启用方法带入参数
System.out.println("请输入要删除课程的编号");
String ID = writeIn();
Statement state;
try {
state = connect.createStatement();
state.execute("delete from course where id = "+ID);
System.out.println("删除完成");
} catch (SQLException e) {
e.printStackTrace();
}finally{
shutDownConnect(connect);//方便给shutDownConnect传参
}
}
public void updateCourse(){
Connection connect = openConnect();//启用方法带入参数
System.out.println("请输入要修改课程的编号");
String ID = writeIn();
System.out.println("请输入修改后课程的名称");
String str = writeIn();
Statement state;
try {
state = connect.createStatement();
state.execute("update course set name=\""+str+"\" where id = "+ID);
System.out.println("修改完成");
} catch (SQLException e) {
e.printStackTrace();
}finally{
shutDownConnect(connect);//方便给shutDownConnect传参
}
}
public void selectCourse(){
String str = "select*from course";
Connection connect = openConnect();
Statement state;
try {
state = connect.createStatement();
ResultSet resultSet = state.executeQuery(str);//游标默认指向第一条记当上方
while(resultSet.next()){
int ID = resultSet.getInt(2);//2表示id的索引从一开始
String Name = resultSet.getString("name");//直接使用列名
System.out.println("id: "+ID+" name "+Name);
}

} catch (SQLException e) {
e.printStackTrace();
}finally{
shutDownConnect(connect);//方便给shutDownConnect传参
}
}
//连接数据库
public Connection openConnect(){
Connection connect = null;
try {
connect = DriverManager.getConnection(url, uer, password);
if(connect != null){
System.out.println("数据库连接成功");
}

} catch (SQLException e) {
System.out.println("数据库连接失败");
}
return connect;
}
//关闭数据库连接
public void shutDownConnect(Connection connect){
if(connect != null){
try {
connect.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
//输入数据
public String writeIn(){
InputStreamReader ins = new InputStreamReader(System.in);
BufferedReader buf = new BufferedReader(ins);
String str= "";
boolean flag = true;
while(flag){
try {
str = buf.readLine();
flag = false;
} catch (IOException e) {
System.out.println("I/o流输入异常");
}catch(NumberFormatException n){
System.out.println("输入数据类型错误,请从新输入");
}
}
return str;
}

public static void main(String[] args){
DbConnection con = new DbConnection();
con.loadDBConnect();
con.addCourse();

}

}

你可能感兴趣的:(如何使用控制台操作数据库)