JDBC连接数据库
来源:互联网 发布:数控铣床编程小酒杯 编辑:程序博客网 时间:2024/04/29 13:36
经过这段时间的学习,整理了一下JDBC的基础知识,与新入手jsp的小伙伴分享一下,刚开始学,变学习边给大家整理吧!
小常识:关于连接JDBC驱动,常见的有以下两种:
1.连接数据库
尽量晚点建立Connection,早点关闭,可以减轻数据库负担
public class JDBCTest {
public static void main(String[] args) throws ClassNotFoundException, SQLException {//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@localhost:1521:orcl";
String user="system";
String password="orcl";
Connection con=null;
//创建连接
con=DriverManager.getConnection(url, user, password);
System.out.println("数据库连接成功!");
//释放资源
con.close();
}
}
》》》》》至此 数据库连接成功!
2.查询数据库中的某一个表的内容
public class JDBCTest {
public static void main(String[] args) throws ClassNotFoundException, SQLException {//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@localhost:1521:orcl";
String user="system";
String password="orcl";
Connection con=null;
//创建连接
con=DriverManager.getConnection(url, user, password);
System.out.println("数据库连接成功!");
//创建(SQL)语句
Statement stmt=con.createStatement();
/**
*String sql="select * from books";
*ResultSet rs=stmt.executeQuery("sql");
*/
注意:在Eclipse中输入select * from 一定要用空格隔开,否则运行时会报错;在Oracle中分不分开不影响运行结果
//执行语句
ResultSet rs=stmt.executeQuery("select * from books"); //rs是结果集,查询出一个列表
while(rs.next()){ //每rs.next()一次指针都会向后移动一位,指向下一条记录
System.out.println(rs.getObject(1)+"/t"+rs.getObject(2)+"\t"); //getObject()获得列
}
//释放资源
rs.close();
stmt.close();
con.close();
}
}
》》》》》运行结果显示出books表的第一列、第二列。
3.修改数据库中的某一个表的内容
将books表的bookId=1003的书名改为狂人日记。
public class Base_5 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "system", "orcl");
Statement stmt=con.createStatement();
String sql="update books set booksName='狂人日记' where bookId=1003";
int num=stmt.executeUpdate(sql); //返回值为受影响的行数
System.out.println("num="+num);
}
}
4.添加数据库中的某一个表的内容
public class Base_3 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "system", "orcl");
Statement stmt=con.createStatement();
//输入接收的数据
Scanner input=new Scanner(System.in);
System.out.println("请输入数的编号:");
int bookId=input.nextInt();
System.out.println("请输入书名:");
String booksName=input.next();
System.out.println("请输入作者编号:");
String authorId=input.next();
System.out.println("请输入图书分类号:");
int cateId=input.nextInt();
System.out.println("请输入图书价格:");
float price=input.nextFloat();
System.out.println("请输入出版社编号:");
int publishId=input.nextInt();
/*String sql="insert into books values("
+ "1004,"
+ "'三毛流浪记',"
+ "'a2',"
+ "2,"
+ "15.8,"
+ "1)";*/
//String sql="select * from books";
StringBuffer sql=new StringBuffer("insert into books values(");
sql.append(bookId+",");
sql.append("'"+booksName+"',");
sql.append("'"+authorId+"',");
sql.append(cateId+",");
sql.append(price+",");
sql.append(publishId+")");
boolean torf=stmt.execute(sql.toString());
System.out.println(torf); //false: 表示增删改; 若为true:表示查询结合
stmt.close();
con.close();
}
}
4.删除数据库中的某一个表的某一行的内容
public class Base_4 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "system", "orcl");
Statement stmt=con.createStatement();
String sql="delete from books where bookId=1005";
int num=stmt.executeUpdate(sql);
System.out.println("num="+num);
stmt.close();
con.close();
}
}
- jdbc入门、jdbc连接数据库
- jdbc-odbc连接数据库
- JDBC连接MySQL数据库
- JDBC连接MySQL数据库
- JDBC之连接数据库
- [JDBC] 如何连接数据库
- JDBC连接数据库技巧
- jdbc连接数据库
- JDBC连接常用数据库
- jdbc连接数据库
- JDBC连接SqlServer2005数据库
- jdbc连接各种数据库
- JDBC连接各种数据库
- JDBC连接数据库参考
- JDBC连接数据库
- JDBC连接数据库小结
- JDBC连接MYSQL数据库
- Jdbc 连接数据库 实例
- JavaScript学习笔记--03
- [高斯消元 线性基 二进制拆分] BZOJ 2844 albus就是要第一个出场
- 数学规律题,数论知识:hdu1792
- iOS开发工具----Cocoapods的安装与使用
- dos 运行php文件丢失拓展模块
- JDBC连接数据库
- Mysql 主从架构之-安装与配置
- JS将EXCEL文件导入前端第一行未出现或者出现F1F2F3的解决方法……
- Mesos+Docker+Marathon环境搭建教程
- hadoop测试
- 对象属性的遍历
- shell编程中for,while,util,case,select,break, continue用法详解
- u-boot移植全过程
- 无废话WCF入门教程三[WCF的宿主]