Java tutorial Java API simulator Learning

Source: Internet
Author: User

Package zmsjdbc;
Import java. SQL .*;
Public class mysql1
{

Private string url = "JDBC: mysql: // localhost: 3306/beyond ";
// First download MySQL driver mm. mysql-2.0.4-bin.jar in D: \ jbuilder9 \ jdk1.4 \ JRE \ Lib \ ext

Private string user = "root ";
Private string Pwd = "";
Public connection getcon ()
{
Connection con;
Try
{// Load the driverProgram
Class. forname ("org. gjt. Mm. MySQL. Driver"). newinstance ();
// Create a connection
Con = drivermanager. getconnection (URL, user, PWD );
Return con;
}

Catch (classnotfoundexception E)
{
System. Out. println ("Driver loading error ");
}

Catch (exception E)
{
System. Out. println ("A big error occurred ");
}

Return NULL;
}
Public resultset executesql (string sqlstr)
{
Connection conn;
Statement stmt;

Try
{
Conn = getcon ();
Stmt = conn. createstatement ();
Resultset rs1_stmt.exe cutequery (sqlstr );

Return Rs;
}
Catch (sqlexception E)
{
System. Out. Print ("An error occurred while obtaining data ");
}
Return NULL;
}

Public static void main (string [] ARGs)
{
Mysql1 mysql11 = new mysql1 ();
Connection conn;
Resultset RS;
Rs = mysql11.executesql ("select * From zms1 ");
Try
{
While (Rs. Next ())
{
System. Out. println (Rs. getstring ("name "));
}
}
Catch (exception E)
{
System. Out. println (E. getmessage ());
}

}

}



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.