美文网首页
Java工程连接数据库

Java工程连接数据库

作者: 小鱼儿_f32a | 来源:发表于2019-07-22 11:20 被阅读0次

    package com.sinosoft.test;

    import java.sql.*;

    public class DBTest {

    //mysql驱动包名

        private static final StringDRIVER_NAME ="com.mysql.jdbc.Driver";

    //数据库连接地址

        private static final StringURL ="jdbc:mysql://IP(具体IP):3306/数据库名()?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8&zeroDateTimeBehavior=convertToNull";

    //用户名

        private static final StringUSER_NAME ="数据库账户名";

    //密码

        private static final StringPASSWORD ="密码";

    public static void main(String[] args) {

    Connection connection =null;

    try {

    //加载mysql的驱动类

                Class.forName(DRIVER_NAME);

    //获取数据库连接

                connection = DriverManager.getConnection(URL,USER_NAME,PASSWORD);

    //mysql查询语句

                String sql ="SELECT productname FROM lrproduct";

    PreparedStatement prst = connection.prepareStatement(sql);

    //结果集

                ResultSet rs = prst.executeQuery();

    while (rs.next()) {

    System.out.println("用户名:" + rs.getString("productname"));

    }

    rs.close();

    prst.close();

    }catch (Exception e) {

    e.printStackTrace();

    }finally {

    if (connection !=null) {

    try {

    connection.close();

    }catch (SQLException e) {

    e.printStackTrace();

    }

    }

    }

        }

    }

    jdbc

    https://blog.csdn.net/u012075383/article/details/78254482

    https://blog.csdn.net/u014631593/article/details/52791391

    相关文章

      网友评论

          本文标题:Java工程连接数据库

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