My fifth program Java JDBC Connection MySQL database implementation input Query

Source: Internet
Author: User

ImportJava.sql.*;ImportJava.util.Scanner; Public classJdbctest { Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); System.out.println ("Please enter the name you want to query:"); String Search_name=Sc.next (); /**************************** Connection Configuration, all string types *********************************/                       //driver nameString Driver = "Com.mysql.jdbc.Driver"; //the URL points to the database name you want to access ScutcsString url = "Jdbc:mysql://127.0.0.1:3306/scutcs"; //user name when MySQL is configuredString user = "root"; //password for MySQL configurationString password = "root"; /**************************** Open Connection Database *********************************/           Try {             //Load DriverClass.forName (driver); //Continuous DatabaseConnection conn =drivermanager.getconnection (URL, user, password); if(!conn.isclosed ()) System.out.println ("Succeeded connecting to the database!"); //statement used to execute SQL statementsStatement Statement =conn.createstatement (); //the SQL statement to executeString sql = "SELECT * from student where sname= '" "+search_name+" "; //result setResultSet rs =statement.executequery (SQL); System.out.println ("----------------------------------"); System.out.println (The results of the execution are as follows:); System.out.println ("----------------------------------"); System.out.println ("School Number" + "\ T" + "name" + "\ T" + "gender"); System.out.println ("----------------------------------"); String name=NULL; String Sex=NULL;  while(Rs.next ()) {//Select sname This column of dataName = Rs.getstring ("sname"); //Choose sex for this column of dataSex = rs.getstring ("Sex"); //first, use the iso-8859-1 character set to decode name into a sequence of bytes and store the result in a new byte array. //the specified byte array is then decoded using the GB2312 character set//name = new String (name.getbytes ("iso-8859-1"), "GB2312");Name =NewString (name); Sex=NewString (Sex); //Output ResultsSystem.out.println (rs.getstring ("sno") + "\ T" + name+ "\ T" +sex);            } rs.close ();           Conn.close (); } Catch(ClassNotFoundException e) {System.out.println ("Sorry,can ' t find the driver!");           E.printstacktrace (); } Catch(SQLException e) {e.printstacktrace (); } Catch(Exception e) {e.printstacktrace (); } } }

My fifth program Java JDBC Connection MySQL database implementation input Query

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.