jdbc connection


import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBC {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Connection conn = null;
        try {
            String url = "jdbc:mysql://localhost:3306/mysql";
            
            //user:登录数据库的用户名
            String user = "root"; //自己的账号
            //password:用户名对应的密码,这些都是自己之前设定的
            String password = "1234"; //自己的密码
            //mySql的驱动:com.mysql.jdbc.Driver
            String driverName = "com.mysql.jdbc.Driver";
             
            //2.实例化Driver
            Class clazz = Class.forName(driverName);
            Driver driver = (Driver) clazz.newInstance();
             
            //3.通过DriverManager来注册驱动
            DriverManager.registerDriver(driver);
            //4.通过DriverManager的getConnection方法,获取Connection类的对象
            conn = DriverManager.getConnection(url, user, password);

            selectTable(conn);
            String insert_sql = "insert into student values('scofield',45,89,100)";
            PreparedStatement preparedStatement = conn.prepareStatement(insert_sql);
            int row = preparedStatement.executeUpdate(insert_sql);
            if (row==1) {
                System.out.println("insert successfully!");
                selectTable(conn);
            }
            else if(row==0) {
                System.out.println("insert unsuccessfully!");
            }
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                //当conn不为空时
                if(conn != null) 
                    //关闭conn资源
                    conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
		

	}
	public static void selectTable(Connection conn) throws SQLException {
        System.out.println("select student:");
		String sql = "select * from student";
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        //执行语句
        /**/
        ResultSet resultSet = preparedStatement.executeQuery(sql);
        //返回地数据存储在result中
        System.out.print("[");
        System.out.print("name\t");
        System.out.print("English\t");
        System.out.print("Math\t");
        System.out.print("Computer");
        System.out.println("]");
        while (resultSet.next()){
            System.out.print("[");
            System.out.print( resultSet.getString("name")+"\t");
            System.out.print( resultSet.getInt("English")+"\t");
            System.out.print( resultSet.getInt("Math")+"\t");
            System.out.print( resultSet.getInt("Computer")+"\t");
            System.out.println("]");
        }
        if (resultSet != null){
            resultSet.close();
        }
        if(preparedStatement != null){
            preparedStatement.close();
        }
	}

}

(2)


import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBC {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Connection conn = null;
        try {
            String url = "jdbc:mysql://localhost:3306/mysql";
            
            //user:登录数据库的用户名
            String user = "root"; //自己的账号
            //password:用户名对应的密码,这些都是自己之前设定的
            String password = "1234"; //自己的密码
            //mySql的驱动:com.mysql.jdbc.Driver
            String driverName = "com.mysql.jdbc.Driver";
             
            //2.实例化Driver
            Class clazz = Class.forName(driverName);
            Driver driver = (Driver) clazz.newInstance();
             
            //3.通过DriverManager来注册驱动
            DriverManager.registerDriver(driver);
            //4.通过DriverManager的getConnection方法,获取Connection类的对象
            conn = DriverManager.getConnection(url, user, password);

            selectTable(conn);
//            String insert_sql = "insert into student values('scofield',45,89,100)";
//            PreparedStatement preparedStatement = conn.prepareStatement(insert_sql);
//            int row = preparedStatement.executeUpdate(insert_sql);
//            if (row==1) {
//                System.out.println("insert successfully!");
//                selectTable(conn);
//            }
//            else if(row==0) {
//                System.out.println("insert unsuccessfully!");
//            }
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                //当conn不为空时
                if(conn != null) 
                    //关闭conn资源
                    conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
		

	}
	public static void selectTable(Connection conn) throws SQLException {
        System.out.println("select student:");
//        String sql = "select * from student";
		String sql = "select name,English from student where name='scofield'";
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        //执行语句
        /**/
        ResultSet resultSet = preparedStatement.executeQuery(sql);
        //返回地数据存储在result中
        System.out.print("[");
        System.out.print("name\t");
        System.out.print("English\t");
//        System.out.print("Math\t");
//        System.out.print("Computer");
        System.out.println("]");
        while (resultSet.next()){
            System.out.print("[");
            System.out.print( resultSet.getString("name")+"\t");
            System.out.print( resultSet.getInt("English")+"\t");
//            System.out.print( resultSet.getInt("Math")+"\t");
//            System.out.print( resultSet.getInt("Computer")+"\t");
            System.out.println("]");
        }
        if (resultSet != null){
            resultSet.close();
        }
        if(preparedStatement != null){
            preparedStatement.close();
        }
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值