美文网首页
2019-03-28 JDBC

2019-03-28 JDBC

作者: 沫忘丶 | 来源:发表于2019-03-28 16:51 被阅读0次

public class JDBCDemo1 {

public static void main(String[] args) {

//1.加载数据库驱动程序(需要把驱动加载到方法区)

Connection conn =null;

PreparedStatement ps = null;

ResultSet rs =null;

try {

//1.加载数据库驱动程序(需要把驱动加载到方法区) 可以省略

// Class.forName("com.mysql.jdbc.Driver");

// 2.利用驱动管理器获取数据库连接

// localhost 本地:如果需要连接他人数据库 修改成对方ip

// 3306 端口号 是mysql默认端口号

// jdbc 数据库名称(不是连接名称)

// useUnicode=true&characterEncoding=utf8 支持中文

String url = "jdbc:mysql://localhost:3306/javademo?useUnicode=true&characterEncoding=utf8";

String user = "root";

String password = "root";

conn = DriverManager.getConnection(url,user,password);

//3.获取SQL语句对象

String sql = "select * from dept";

ps = conn.prepareStatement(sql);

//4.执行语句

rs = ps.executeQuery();

while(rs.next()) {

//获取编号

int deptno = rs.getInt("deptno");

//获取部门名称

String dname = rs.getString("dname");

//获取工作地点

String loc = rs.getString("loc");

System.out.println(deptno+","+dname+","+loc);

}

} catch (SQLException e) {

e.printStackTrace();

}

finally {

if(conn != null) {

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

if(ps != null) {

try {

ps.close();

} catch (SQLException e) {

e.printStackTrace();

}

if(rs != null) {

try {

rs.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

}

}

}

public class JDBCDemo2 {

/**

* 查询emp表中的empno,ename,sal,hiredate在控制台显示

* @param args

*/

public static void main(String[] args) {

Connection conn = null;

PreparedStatement ps =null;

ResultSet rs =null;

try {

String url = "jdbc:mysql://localhost:3306/javademo?useUnicode=true&characterEncoding=utf8";

String user = "root";

String password = "root";

conn = DriverManager.getConnection(url, user, password);

String sql = "select * from emp";

ps = conn.prepareStatement(sql);

rs = ps.executeQuery();

while(rs.next()) {

int empno = rs.getInt("empno");

String ename = rs.getString("ename");

double sal = rs.getDouble("sal");

String hiredate = rs.getString("hiredate");

System.out.println(empno+","+ename+","+sal+","+hiredate);

}

} catch (SQLException e) {

e.printStackTrace();

}

finally {

if(conn != null) {

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if(ps != null) {

try {

ps.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if(rs != null) {

try {

rs.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

}

/**

* 查询emp表中的empno,ename,sal,hiredate在控制台显示

* @param args

*/

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("请输入要查询的部门代码:");

int dnum = scan.nextInt();

Connection conn = null;

PreparedStatement ps =null;

ResultSet rs =null;

try {

String url = "jdbc:mysql://localhost:3306/javademo?useUnicode=true&characterEncoding=utf8";

String user = "root";

String password = "root";

conn = DriverManager.getConnection(url, user, password);

String sql = "select * from emp where deptno =?";

ps = conn.prepareStatement(sql);

ps.setInt(1,dnum);

rs = ps.executeQuery();

while(rs.next()) {

int empno = rs.getInt("empno");

String ename = rs.getString("ename");

double sal = rs.getDouble("sal");

String hiredate = rs.getString("hiredate");

System.out.println(empno+","+ename+","+sal+","+hiredate);

}

} catch (SQLException e) {

e.printStackTrace();

}

finally {

if(conn != null) {

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if(ps != null) {

try {

ps.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if(rs != null) {

try {

rs.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

}

/**

* 查询emp表中姓名包含S的员工姓名,工资

* @param args

*/

public static void main(String[] args) {

Connection conn = null;

PreparedStatement ps =null;

ResultSet rs =null;

try {

String url = "jdbc:mysql://localhost:3306/javademo?useUnicode=true&characterEncoding=utf8";

String user = "root";

String password = "root";

conn = DriverManager.getConnection(url, user, password);

String sql = "select ename,sal from emp where ename like '%S%' ";

ps = conn.prepareStatement(sql);

rs = ps.executeQuery();

while(rs.next()) {

String ename = rs.getString("ename");

double sal = rs.getDouble("sal");

System.out.println(ename+","+sal);

}

} catch (SQLException e) {

e.printStackTrace();

}

finally {

if(conn != null) {

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if(ps != null) {

try {

ps.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if(rs != null) {

try {

rs.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

}

相关文章

  • 2019-03-28 JDBC

    public class JDBCDemo1 { public static void main(String[]...

  • day4

    2019-03-28

  • 四、spring加载之refresh过程(3)-invokeBe

    日期:2019-03-28 备注:refresh主流程 ​ ...

  • 2019-03-28精灵作业

    2019-03-28 项目 body{ margin:0; padding:0; ...

  • JDBC

    JDBC原理: JDBC: 抽取JDBC工具类 : JDBCUtils JDBC控制事务:

  • HEXO

    title: Hello Worlddate: 2019-03-28 08:22:18comments: true...

  • JDBC 的使用

    JDBC JDBC什么是JDBCJDBC 的使用JDBC 的数据类型 什么是JDBC JDBC(Java Data...

  • Java和MySQL简建立连接

    JDBC JDBC插入多条数据 JDBC查询 JDBC动态传参 JDBC回滚 JDBC将数据库中的信息装入List...

  • JDBC

    JDBC JDBC:Java DataBase Connectivity JDBC的本质是什么?JDBC是SUN公...

  • java异常合集

    jdbc com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorExce...

网友评论

      本文标题:2019-03-28 JDBC

      本文链接:https://www.haomeiwen.com/subject/yskcbqtx.html