/******************** test using temporary data source code ***************/string URL = "Jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "root"; Connection con = drivermanager.getconnection (URL, username, password); /********************JDBC Connection Database Operation ********************************/<span style= "White-space:pre" ></span >//(1) First load the driver of the database you want to connect to the JVM (Java Virtual machine), <span style= "White-space:pre" ></span>// This is achieved Class.forName ("Com.mysql.jdbc.Driver") through the static method forname (String className) of the Java.lang.Class class, and/or (2) obtaining a database connection/*** for example: ( MySQL connection URL) localhost:3306/test?useunicode=true&characterencoding=gbk; Useunicode=true: Indicates the use of the Unicode character set. If Characterencoding is set to gb2312 or GBK, this parameter must be set to true. CHARACTERENCODING=GBK: The character encoding method. /string url = "Jdbc:mysql://localhost:3306/test"; String username = "root"; String password = "root"; Connection con = drivermanager.getconnection (URL, username, password); (3) Create Statement object Statement stmt = Con.createstatement (); Normal state.The ment object is not recommended for use. PreparedStatement pstmt = con.preparestatement (sql); can prevent SQL injection callablestatement cstmt = Con.preparecall ("{Call Demosp (?,?)}"); Call the stored procedure//(4) Execute SQL statement String sqlstring= "SQL"; ResultSet rs = pstmt.executequery (sqlString); Executes the SQL statement that queries the database, returning a result set (ResultSet) object. int rows = pstmt.executeupdate (sqlString); Used to perform INSERT, UPDATE, or DELETE statements, and so on. (5) Processing result set while (Rs.next ()) {String name = rs.getstring ("name"); String pass = rs.getstring (1); This method is more efficient}
JDBC Database connection