如何在Java se中关联MySql数据库

我采用的是JDBC的方式。通过JDBC与一个数据库建立连接,向已连接的数据库发送SQL语句、处理SQL语句的返回结果。步骤如下:

1.加载驱动:Class.forName("com.mysql.jdbc.Driver");

2.得到连接(指定连接到哪个数据源,用户名和密码)

String url = "jdbc:mysql://localhost:3306/abccs?user=root&password=123456";

Connection conn = DriverManager.getConnection(url);

注:其中3306是Mysql的端口号,abccs是数据库名,user=root指用户名是root,password=123456指密码是123456

3.创建Preparedstatement对象,发送SQL语句

sql= "insert into loading values(?,?,?)";

Preparedstatement  ps=conn.prepareStatement(sql);

注:?防止SQL注入。

4.执行(crud)

//ResultSet 结果集 rs指向结果集第一行的前一行
ResultSet rs = ps.executeQuery();(SQL中的查询语句)

ps.executeUpdate();(SQL中的更新、插入和删除语句)

附上代码:

/**

*功能:实现创建数据库abccs

*/

public class Test2 {
 
    public static void main(String[] args) {
    
        Connection ct=null;
        PreparedStatement ps=null;
        ResultSet rs = null;
        String url = "jdbc:mysql://localhost:3306/abccs?"
                + "user=root&password=123456";

        try {

             //加载驱动

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

            //创建连接

            ct=DriverManager.getConnection(url);
            ps=ct.prepareStatement("create database abcd");
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                if(rs!=null){
                    rs.close();
                }
                if(ps!=null){
                    ps.close();
                }
                if(ct!=null){
                    ct.close();
                }
                
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }

}

Java 实现 mysql 多表关联查询需要使用 SQL 语句。可以使用 JDBC 连接到 MySQL 数据库,并执行 SQL 查询语句。 下面是一个简单的示例代码,展示了如何在 Java 执行多表关联查询: ```java import java.sql.*; public class MySQLExample { static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/your_database_name"; static final String USER = "your_username"; static final String PASS = "your_password"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try { Class.forName(JDBC_DRIVER); System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL,USER,PASS); System.out.println("Creating statement..."); stmt = conn.createStatement(); String sql = "SELECT employees.name, departments.dept_name FROM employees " + "INNER JOIN departments ON employees.dept_id = departments.dept_id"; ResultSet rs = stmt.executeQuery(sql); while(rs.next()){ String name = rs.getString("name"); String deptName = rs.getString("dept_name"); System.out.print("Name: " + name); System.out.println(", Department: " + deptName); } rs.close(); stmt.close(); conn.close(); } catch(SQLException se) { se.printStackTrace(); } catch(Exception e) { e.printStackTrace(); } finally { try { if(stmt!=null) stmt.close(); } catch(SQLException se2) { } try { if(conn!=null) conn.close(); } catch(SQLException se) { se.printStackTrace(); } } System.out.println("Goodbye!"); } } ``` 在上面的代码,我们创建了一个连接到 MySQL 数据库的 Connection 对象。然后,我们创建一个 Statement 对象,并使用 SQL 语句执行多表关联查询。 查询结果将被存储在 ResultSet 对象,我们使用该对象获取查询结果并进行处理。 最后,我们关闭 ResultSet、Statement 和 Connection 对象,释放资源。 注意,上面的代码只是一个简单的示例。在实际开发,我们应该考虑异常处理、连接池、性能优化等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值